-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
obfs.go
62 lines (55 loc) · 1.4 KB
/
obfs.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sip003
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/transport/simple-obfs"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ Plugin = (*ObfsLocal)(nil)
func init() {
RegisterPlugin("obfs-local", newObfsLocal)
}
func newObfsLocal(ctx context.Context, pluginOpts Args, router adapter.Router, dialer N.Dialer, serverAddr M.Socksaddr) (Plugin, error) {
plugin := &ObfsLocal{
dialer: dialer,
serverAddr: serverAddr,
}
mode := "http"
if obfsMode, loaded := pluginOpts.Get("obfs"); loaded {
mode = obfsMode
}
if obfsHost, loaded := pluginOpts.Get("obfs-host"); loaded {
plugin.host = obfsHost
}
switch mode {
case "http":
case "tls":
plugin.tls = true
default:
return nil, E.New("unknown obfs mode ", mode)
}
plugin.port = F.ToString(serverAddr.Port)
return plugin, nil
}
type ObfsLocal struct {
dialer N.Dialer
serverAddr M.Socksaddr
tls bool
host string
port string
}
func (o *ObfsLocal) DialContext(ctx context.Context) (net.Conn, error) {
conn, err := o.dialer.DialContext(ctx, N.NetworkTCP, o.serverAddr)
if err != nil {
return nil, err
}
if !o.tls {
return obfs.NewHTTPObfs(conn, o.host, o.port), nil
} else {
return obfs.NewTLSObfs(conn, o.host), nil
}
}