github.com/sagernet/sing-box@v1.9.0-rc.20/transport/sip003/obfs.go (about) 1 package sip003 2 3 import ( 4 "context" 5 "net" 6 7 "github.com/sagernet/sing-box/adapter" 8 "github.com/sagernet/sing-box/transport/simple-obfs" 9 E "github.com/sagernet/sing/common/exceptions" 10 F "github.com/sagernet/sing/common/format" 11 M "github.com/sagernet/sing/common/metadata" 12 N "github.com/sagernet/sing/common/network" 13 ) 14 15 var _ Plugin = (*ObfsLocal)(nil) 16 17 func init() { 18 RegisterPlugin("obfs-local", newObfsLocal) 19 } 20 21 func newObfsLocal(ctx context.Context, pluginOpts Args, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error) { 22 plugin := &ObfsLocal{ 23 dialer: dialer, 24 serverAddr: serverAddr, 25 } 26 mode := "http" 27 if obfsMode, loaded := pluginOpts.Get("obfs"); loaded { 28 mode = obfsMode 29 } 30 if obfsHost, loaded := pluginOpts.Get("obfs-host"); loaded { 31 plugin.host = obfsHost 32 } 33 switch mode { 34 case "http": 35 case "tls": 36 plugin.tls = true 37 default: 38 return nil, E.New("unknown obfs mode ", mode) 39 } 40 plugin.port = F.ToString(serverAddr.Port) 41 return plugin, nil 42 } 43 44 type ObfsLocal struct { 45 dialer N.Dialer 46 serverAddr M.Socksaddr 47 tls bool 48 host string 49 port string 50 } 51 52 func (o *ObfsLocal) DialContext(ctx context.Context) (net.Conn, error) { 53 conn, err := o.dialer.DialContext(ctx, N.NetworkTCP, o.serverAddr) 54 if err != nil { 55 return nil, err 56 } 57 if !o.tls { 58 return obfs.NewHTTPObfs(conn, o.host, o.port), nil 59 } else { 60 return obfs.NewTLSObfs(conn, o.host), nil 61 } 62 }