Files
trojan-go/proxy/nat/nat.go
T
LoyalsoldierandGitHub 2dc60f52e7 Chore: format code & update dependencies (#385)
* Chore: check Go modules before test & update dependencies
* Chore: run go fmt to format code
2021-09-14 15:41:24 +08:00

47 lines
1.2 KiB
Go

//go:build linux
// +build linux
package nat
import (
"context"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/config"
"github.com/p4gefau1t/trojan-go/proxy"
"github.com/p4gefau1t/trojan-go/proxy/client"
"github.com/p4gefau1t/trojan-go/tunnel"
"github.com/p4gefau1t/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)
})
}