forked from p4gefau1t/trojan-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nat.go
46 lines (40 loc) · 1.18 KB
/
nat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//go:build linux
// +build linux
package nat
import (
"context"
"github.com/faireal/trojan-go/common"
"github.com/faireal/trojan-go/config"
"github.com/faireal/trojan-go/proxy"
"github.com/faireal/trojan-go/proxy/client"
"github.com/faireal/trojan-go/tunnel"
"github.com/faireal/trojan-go/tunnel/tproxy"
)
const Name = "NAT"
func init() {
proxy.RegisterProxyCreator(Name, func(ctx context.Context) (*proxy.Proxy, error) {
cfg := config.FromContext(ctx, Name).(*client.Config)
if cfg.Router.Enabled {
return nil, common.NewError("router is not allowed in nat mode")
}
ctx, cancel := context.WithCancel(ctx)
serverStack := []string{tproxy.Name}
clientStack := client.GenerateClientTree(cfg.TransportPlugin.Enabled, cfg.Mux.Enabled, cfg.Websocket.Enabled, cfg.Shadowsocks.Enabled, false)
c, err := proxy.CreateClientStack(ctx, clientStack)
if err != nil {
cancel()
return nil, err
}
s, err := proxy.CreateServerStack(ctx, serverStack)
if err != nil {
cancel()
return nil, err
}
return proxy.NewProxy(ctx, cancel, []tunnel.Server{s}, c), nil
})
}
func init() {
config.RegisterConfigCreator(Name, func() interface{} {
return new(client.Config)
})
}