Skip to content

Commit b8a8e90

Browse files
committed
feat: add http server
1 parent a14ae5e commit b8a8e90

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v1.0-alpha8
4+
5+
- Add a new service type: embedded_http_proxy
6+
37
## v1.0-alpha7
48

59
- Simply print error on failed preflight script instead of executing `printf`

config.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ bind = "tcp://127.0.0.1:10690"
6767
## generate that using "txeh" and "mkcert"
6868
#certificate = "cert/unbound-ssh.local-key.pem"
6969

70+
#[[service]]
71+
## embedded_http_proxy service is a simple http proxy service
72+
#type = "embedded_http_proxy"
73+
#bind = "tcp://127.0.0.1:10692"
74+
7075
#[[service]]
7176
## port_forward service is a simple port forwarding service
7277
#type = "port_forward"
73-
#bind = "tcp://127.0.0.1:10692"
78+
#bind = "tcp://127.0.0.1:10693"
7479
#destination = "tcp://httpbin.org:80"

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9
1111
github.com/antonfisher/nested-logrus-formatter v1.3.1
1212
github.com/creack/pty v1.1.21
13+
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5
1314
github.com/gliderlabs/ssh v0.3.7
1415
github.com/google/uuid v1.6.0
1516
github.com/hashicorp/yamux v0.1.1

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1717
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1818
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1919
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
20+
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5 h1:m62nsMU279qRD9PQSWD1l66kmkXzuYcnVJqL4XLeV2M=
21+
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
22+
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
23+
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
2024
github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
2125
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
2226
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -45,6 +49,7 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
4549
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4650
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab h1:ZjX6I48eZSFetPb41dHudEyVr5v953N15TsNZXlkcWY=
4751
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab/go.mod h1:/PfPXh0EntGc3QAAyUaviy4S9tzy4Zp0e2ilq4voC6E=
52+
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
4853
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
4954
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
5055
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=

internal/config/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ func (s *CodecType) UnmarshalText(text []byte) error {
9494
type ServiceType string
9595

9696
const (
97-
EmbeddedWebdav ServiceType = "embedded_webdav"
98-
EmbeddedSsh ServiceType = "embedded_ssh"
99-
PortForward ServiceType = "port_forward"
100-
Echo ServiceType = "echo"
97+
EmbeddedWebdav ServiceType = "embedded_webdav"
98+
EmbeddedSsh ServiceType = "embedded_ssh"
99+
EmbeddedHttpProxy ServiceType = "embedded_http_proxy"
100+
PortForward ServiceType = "port_forward"
101+
Echo ServiceType = "echo"
101102
)
102103

103104
func (s *ServiceType) UnmarshalText(text []byte) error {
104-
validValues := []ServiceType{EmbeddedWebdav, EmbeddedSsh, PortForward, Echo}
105+
validValues := []ServiceType{EmbeddedWebdav, EmbeddedSsh, EmbeddedHttpProxy, PortForward, Echo}
105106
serviceType := ServiceType(text)
106107
if !lo.Contains(validValues, serviceType) {
107108
return fmt.Errorf("invalid service type: %s", text)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package service
2+
3+
import (
4+
"github.com/elazarl/goproxy"
5+
"github.com/ztrue/tracerr"
6+
"net"
7+
"net/http"
8+
)
9+
10+
type httpServer struct {
11+
server *http.Server
12+
l net.Listener
13+
}
14+
15+
func CreateHttpServer() (Server, error) {
16+
proxy := httpServer{
17+
server: &http.Server{
18+
Handler: goproxy.NewProxyHttpServer(),
19+
},
20+
}
21+
return &proxy, nil
22+
}
23+
24+
func (f *httpServer) Serve(l net.Listener) error {
25+
f.l = l
26+
if err := f.server.Serve(l); err != nil {
27+
return err
28+
}
29+
return nil
30+
}
31+
32+
func (f *httpServer) Close() (err error) {
33+
errs := make([]error, 0)
34+
35+
if err = f.l.Close(); err != nil {
36+
errs = append(errs, err)
37+
}
38+
39+
if err = f.server.Close(); err != nil {
40+
errs = append(errs, err)
41+
}
42+
43+
if len(errs) > 0 {
44+
return tracerr.Errorf("failed to close http proxy server: %v", errs)
45+
} else {
46+
return nil
47+
}
48+
}

internal/service/server_type_webdav.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"github.com/sirupsen/logrus"
5+
"github.com/ztrue/tracerr"
56
"golang.org/x/net/webdav"
67
"net"
78
"net/http"
@@ -51,5 +52,9 @@ func (f *WebdavServer) Close() (err error) {
5152
errs = append(errs, err)
5253
}
5354

54-
return nil
55+
if len(errs) > 0 {
56+
return tracerr.Errorf("failed to close http proxy server: %v", errs)
57+
} else {
58+
return nil
59+
}
5560
}

internal/service/service_manager_spy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func (sm *SpyServiceManager) justLaunch(idx int) (net.Addr, Server, error) {
8282
var server Server
8383
if service.Type == config.EmbeddedWebdav {
8484
server, err = CreateWebdavServer()
85+
} else if service.Type == config.EmbeddedHttpProxy {
86+
server, err = CreateHttpServer()
8587
} else if service.Type == config.EmbeddedSsh {
8688
server, err = CreateSshServer(service.Certificate)
8789
} else if service.Type == config.Echo {

0 commit comments

Comments
 (0)