summaryrefslogtreecommitdiff
path: root/internal/config/outbounds.go
blob: 582b625481da616cfdeb43d5c8af4dc1aee67291 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package config

import "vpnem/internal/models"

type InboundConfig map[string]any

func BuildOutbound(server models.Server) map[string]any {
	return BuildOutboundWithTag(server, "proxy")
}

func BuildOutboundWithTag(server models.Server, tag string) map[string]any {
	switch server.Type {
	case "vless":
		return buildVLESSOutbound(server, tag)
	case "vless-reality":
		return buildVLESSRealityOutbound(server, tag)
	case "vmess":
		return buildVMessOutbound(server, tag)
	case "shadowsocks":
		return buildShadowsocksOutbound(server, tag)
	case "hysteria2":
		return buildHysteria2Outbound(server, tag)
	default:
		return buildSOCKSOutbound(server, tag)
	}
}

func BuildHysteria2Inbound(_ any, port int, password string, obfsPassword string, upMbps int, downMbps int, certPath string, keyPath string) (*InboundConfig, error) {
	if password == "" {
		return nil, errConfig("hysteria2 inbound requires password")
	}
	if certPath == "" || keyPath == "" {
		return nil, errConfig("hysteria2 inbound requires certificate and key paths")
	}
	inbound := InboundConfig{
		"type":        "hysteria2",
		"tag":         "hysteria2-in",
		"listen":      "::",
		"listen_port": port,
		"users": []map[string]any{
			{"name": "user-01", "password": password},
		},
		"tls": map[string]any{
			"enabled":          true,
			"alpn":             []string{"h3"},
			"min_version":      "1.3",
			"max_version":      "1.3",
			"certificate_path": certPath,
			"key_path":         keyPath,
		},
	}
	if upMbps > 0 {
		inbound["up_mbps"] = upMbps
	}
	if downMbps > 0 {
		inbound["down_mbps"] = downMbps
	}
	if obfsPassword != "" {
		inbound["obfs"] = map[string]any{
			"type":     "salamander",
			"password": obfsPassword,
		}
	}
	return &inbound, nil
}

func buildVLESSOutbound(server models.Server, tag string) map[string]any {
	outbound := map[string]any{
		"type": "vless", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort, "uuid": server.UUID,
	}
	applyTLS(outbound, server.TLS)
	applyTransport(outbound, server.Transport)
	return outbound
}

func buildVLESSRealityOutbound(server models.Server, tag string) map[string]any {
	outbound := map[string]any{
		"type": "vless", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort, "uuid": server.UUID,
	}
	applyTLS(outbound, server.TLS)
	return outbound
}

func buildVMessOutbound(server models.Server, tag string) map[string]any {
	outbound := map[string]any{
		"type": "vmess", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort,
		"uuid": server.UUID, "security": "auto", "alter_id": 0,
	}
	applyTLS(outbound, server.TLS)
	applyTransport(outbound, server.Transport)
	return outbound
}

func buildShadowsocksOutbound(server models.Server, tag string) map[string]any {
	return map[string]any{
		"type": "shadowsocks", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort,
		"method": server.Method, "password": server.Password,
	}
}

func buildHysteria2Outbound(server models.Server, tag string) map[string]any {
	outbound := map[string]any{
		"type": "hysteria2", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort,
		"password": server.Password,
	}
	if server.UpMbps > 0 {
		outbound["up_mbps"] = server.UpMbps
	}
	if server.DownMbps > 0 {
		outbound["down_mbps"] = server.DownMbps
	}
	if server.ObfsPassword != "" {
		outbound["obfs"] = map[string]any{"type": "salamander", "password": server.ObfsPassword}
	}
	tlsConfig := map[string]any{
		"enabled":     true,
		"insecure":    true,
		"alpn":        []string{"h3"},
		"min_version": "1.3",
		"max_version": "1.3",
	}
	if server.TLS != nil {
		if server.TLS.ServerName != "" {
			tlsConfig["server_name"] = server.TLS.ServerName
		}
		if len(server.TLS.ALPN) > 0 {
			tlsConfig["alpn"] = server.TLS.ALPN
		}
		if server.TLS.MinVersion != "" {
			tlsConfig["min_version"] = server.TLS.MinVersion
		}
		if server.TLS.MaxVersion != "" {
			tlsConfig["max_version"] = server.TLS.MaxVersion
		}
		if server.TLS.Insecure {
			tlsConfig["insecure"] = true
		}
	}
	outbound["tls"] = tlsConfig
	return outbound
}

func buildSOCKSOutbound(server models.Server, tag string) map[string]any {
	return map[string]any{
		"type": "socks", "tag": tag,
		"server": server.Server, "server_port": server.ServerPort,
		"udp_over_tcp": server.UDPOverTCP,
	}
}

func applyTLS(outbound map[string]any, tls *models.TLS) {
	if tls == nil {
		return
	}
	tlsConfig := map[string]any{
		"enabled":     tls.Enabled,
		"server_name": tls.ServerName,
	}
	if tls.Insecure {
		tlsConfig["insecure"] = true
	}
	if len(tls.ALPN) > 0 {
		tlsConfig["alpn"] = tls.ALPN
	}
	if tls.MinVersion != "" {
		tlsConfig["min_version"] = tls.MinVersion
	}
	if tls.MaxVersion != "" {
		tlsConfig["max_version"] = tls.MaxVersion
	}
	if tls.Reality != nil && tls.Reality.Enabled {
		tlsConfig["reality"] = map[string]any{
			"enabled":    true,
			"public_key": tls.Reality.PublicKey,
			"short_id":   tls.Reality.ShortID,
		}
		if tls.Reality.Fingerprint != "" {
			tlsConfig["utls"] = map[string]any{
				"enabled":     true,
				"fingerprint": tls.Reality.Fingerprint,
			}
		}
	}
	outbound["tls"] = tlsConfig
}

func errConfig(message string) error {
	return &configError{message: message}
}

type configError struct {
	message string
}

func (e *configError) Error() string {
	return e.message
}

func applyTransport(outbound map[string]any, transport *models.Transport) {
	if transport == nil {
		return
	}
	outbound["transport"] = map[string]any{
		"type": transport.Type,
		"path": transport.Path,
	}
}