Skip to content

Commit fa44e5b

Browse files
committed
feat: add http proxy
1 parent dea21c3 commit fa44e5b

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

tunnels/http/httpproxy.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ package http
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/base64"
67
"fmt"
8+
gonet "net"
79
"net/http"
810
"net/url"
911

1012
"github.com/mrhaoxx/OpenNG/net"
1113
)
1214

1315
type HttpProxyInterface struct {
14-
proxyurl *url.URL
15-
underlying net.Interface
16+
Proxyurl *net.URL
1617
}
1718

18-
func (s *HttpProxyInterface) Dial(network, addr string) (net.Conn, error) {
19+
func (s *HttpProxyInterface) DialContext(ctx context.Context, network, addr string) (gonet.Conn, error) {
1920
if network != "tcp" {
2021
return nil, net.ErrTCPOnly
2122
}
2223

23-
c, err := s.underlying.Dial("tcp", s.proxyurl.Host)
24+
c, err := s.Proxyurl.Underlying.DialContext(ctx, "tcp", s.Proxyurl.Host)
2425
if err != nil {
2526
return nil, err
2627
}
@@ -39,8 +40,8 @@ func (s *HttpProxyInterface) Dial(network, addr string) (net.Conn, error) {
3940
}
4041

4142
req.Close = false
42-
if s.proxyurl.User != nil {
43-
req.Header.Set("Proxy-Authorization", "Basic "+basicAuth(s.proxyurl.User.Username(), func() string { pwd, _ := s.proxyurl.User.Password(); return pwd }()))
43+
if s.Proxyurl.User != nil {
44+
req.Header.Set("Proxy-Authorization", "Basic "+basicAuth(s.Proxyurl.User.Username(), func() string { pwd, _ := s.Proxyurl.User.Password(); return pwd }()))
4445
}
4546

4647
req.Header.Set("User-Agent", "OpenNG Proxy Forward")
@@ -66,7 +67,11 @@ func (s *HttpProxyInterface) Dial(network, addr string) (net.Conn, error) {
6667
return c, nil
6768
}
6869

69-
func (s *HttpProxyInterface) Listen(network, address string) (net.Listener, error) {
70+
func (s *HttpProxyInterface) Dial(network, addr string) (gonet.Conn, error) {
71+
return s.DialContext(context.Background(), network, addr)
72+
}
73+
74+
func (s *HttpProxyInterface) Listen(network, address string) (gonet.Listener, error) {
7075
return nil, net.ErrListenNotSupport
7176
}
7277

tunnels/wireguard/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.zx2c4.com/wireguard/device"
1818
"golang.zx2c4.com/wireguard/tun"
1919

20+
"gvisor.dev/gvisor/pkg/tcpip"
2021
gtcp "gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
2122
gudp "gvisor.dev/gvisor/pkg/tcpip/transport/udp"
2223
)
@@ -98,6 +99,7 @@ func NewWireGuardServer(cfg *WireGuardConfig) (*WireGuardServer, error) {
9899
KeepaliveInterval: cfg.TcpKeepaliveInterval,
99100
KeepaliveCount: cfg.TcpKeepAliveCount,
100101
Tnet: tnet,
102+
Addr: tcpip.AddrFrom4(addr.As4()),
101103
StackLock: &lock,
102104
}
103105

tunnels/wireguard/tcp/tcp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Config struct {
4242
KeepaliveCount int
4343
Tnet *netstack.Net
4444
StackLock *sync.Mutex
45+
46+
Addr tcpip.Address
4547
}
4648

4749
// Handler manages a single TCP flow.
@@ -112,6 +114,16 @@ func checkDst(config *Config, s stack.TransportEndpointID) (net.Conn, bool) {
112114
defer cancel()
113115
c, err := net.DefaultRouteTable.DialContext(ctx, "tcp", gnet.JoinHostPort(s.LocalAddress.String(), fmt.Sprint(s.LocalPort)))
114116

117+
// c, err := gonet.DialTCPWithBind(ctx, config.Tnet.Stack(), tcpip.FullAddress{
118+
// NIC: 1,
119+
// Addr: config.Addr,
120+
// Port: 0,
121+
// }, tcpip.FullAddress{
122+
// NIC: 1,
123+
// Addr: s.LocalAddress,
124+
// Port: s.LocalPort,
125+
// }, ipv4.ProtocolNumber)
126+
115127
if err != nil {
116128
// If connection refused, we can send a reset to let peer know.
117129
if oerr, ok := err.(*gnet.OpError); ok {

ui/builtin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,15 @@ var _builtin_refs_assertions = map[string]Assert{
949949
},
950950
},
951951
},
952+
"builtin::http::proxy": {
953+
Type: "map",
954+
Sub: AssertMap{
955+
"url": {
956+
Type: "url",
957+
Required: true,
958+
},
959+
},
960+
},
952961
"builtin::net::interface::sys": {
953962
Type: "null",
954963
Desc: "use system default interface",
@@ -1732,6 +1741,10 @@ var _builtin_refs = map[string]Inst{
17321741
Underlying: underlying,
17331742
}, nil
17341743
},
1744+
"builtin::http::proxy": func(spec *ArgNode) (any, error) {
1745+
proxyurl := spec.MustGet("url").ToURL()
1746+
return &thttp.HttpProxyInterface{Proxyurl: proxyurl}, nil
1747+
},
17351748
"builtin::net::interface::sys": func(*ArgNode) (any, error) {
17361749
return &net.SysInterface{}, nil
17371750
},

0 commit comments

Comments
 (0)