forked from Jigsaw-Code/outline-ss-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
277 lines (251 loc) · 8.13 KB
/
server.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2018 Jigsaw Operations LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"container/list"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/Jigsaw-Code/outline-ss-server/service"
"github.com/Jigsaw-Code/outline-ss-server/service/metrics"
ss "github.com/Jigsaw-Code/outline-ss-server/shadowsocks"
"github.com/op/go-logging"
"github.com/oschwald/geoip2-golang"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/crypto/ssh/terminal"
"gopkg.in/yaml.v2"
)
var logger *logging.Logger
// Set by goreleaser default ldflags. See https://goreleaser.com/customization/build/
var version = "dev"
// 59 seconds is most common timeout for servers that do not respond to invalid requests
const tcpReadTimeout time.Duration = 59 * time.Second
// A UDP NAT timeout of at least 5 minutes is recommended in RFC 4787 Section 4.3.
const defaultNatTimeout time.Duration = 5 * time.Minute
func init() {
var prefix = "%{level:.1s}%{time:2006-01-02T15:04:05.000Z07:00} %{pid} %{shortfile}]"
if terminal.IsTerminal(int(os.Stderr.Fd())) {
// Add color only if the output is the terminal
prefix = strings.Join([]string{"%{color}", prefix, "%{color:reset}"}, "")
}
logging.SetFormatter(logging.MustStringFormatter(strings.Join([]string{prefix, " %{message}"}, "")))
logging.SetBackend(logging.NewLogBackend(os.Stderr, "", 0))
logger = logging.MustGetLogger("")
}
type ssPort struct {
tcpService service.TCPService
udpService service.UDPService
cipherList service.CipherList
}
type SSServer struct {
natTimeout time.Duration
m metrics.ShadowsocksMetrics
replayCache service.ReplayCache
ports map[int]*ssPort
}
func (s *SSServer) startPort(portNum int) error {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: portNum})
if err != nil {
return fmt.Errorf("Failed to start TCP on port %v: %v", portNum, err)
}
packetConn, err := net.ListenUDP("udp", &net.UDPAddr{Port: portNum})
if err != nil {
return fmt.Errorf("Failed to start UDP on port %v: %v", portNum, err)
}
logger.Infof("Listening TCP and UDP on port %v", portNum)
port := &ssPort{cipherList: service.NewCipherList()}
// TODO: Register initial data metrics at zero.
port.tcpService = service.NewTCPService(port.cipherList, &s.replayCache, s.m, tcpReadTimeout)
port.udpService = service.NewUDPService(s.natTimeout, port.cipherList, s.m)
s.ports[portNum] = port
go port.tcpService.Serve(listener)
go port.udpService.Serve(packetConn)
return nil
}
func (s *SSServer) removePort(portNum int) error {
port, ok := s.ports[portNum]
if !ok {
return fmt.Errorf("Port %v doesn't exist", portNum)
}
tcpErr := port.tcpService.Stop()
udpErr := port.udpService.Stop()
delete(s.ports, portNum)
if tcpErr != nil {
return fmt.Errorf("Failed to close listener on %v: %v", portNum, tcpErr)
}
if udpErr != nil {
return fmt.Errorf("Failed to close packetConn on %v: %v", portNum, udpErr)
}
logger.Infof("Stopped TCP and UDP on port %v", portNum)
return nil
}
func (s *SSServer) loadConfig(filename string) error {
config, err := readConfig(filename)
if err != nil {
return fmt.Errorf("Failed to read config file %v: %v", filename, err)
}
portChanges := make(map[int]int)
portCiphers := make(map[int]*list.List) // Values are *List of *CipherEntry.
for _, keyConfig := range config.Keys {
portChanges[keyConfig.Port] = 1
cipherList, ok := portCiphers[keyConfig.Port]
if !ok {
cipherList = list.New()
portCiphers[keyConfig.Port] = cipherList
}
cipher, err := ss.NewCipher(keyConfig.Cipher, keyConfig.Secret)
if err != nil {
return fmt.Errorf("Failed to create cipher for key %v: %v", keyConfig.ID, err)
}
entry := service.MakeCipherEntry(keyConfig.ID, cipher, keyConfig.Secret)
cipherList.PushBack(&entry)
}
for port := range s.ports {
portChanges[port] = portChanges[port] - 1
}
for portNum, count := range portChanges {
if count == -1 {
if err := s.removePort(portNum); err != nil {
return fmt.Errorf("Failed to remove port %v: %v", portNum, err)
}
} else if count == +1 {
if err := s.startPort(portNum); err != nil {
return fmt.Errorf("Failed to start port %v: %v", portNum, err)
}
}
}
for portNum, cipherList := range portCiphers {
s.ports[portNum].cipherList.Update(cipherList)
}
logger.Infof("Loaded %v access keys", len(config.Keys))
s.m.SetNumAccessKeys(len(config.Keys), len(portCiphers))
return nil
}
// Stop serving on all ports.
func (s *SSServer) Stop() error {
for portNum := range s.ports {
if err := s.removePort(portNum); err != nil {
return err
}
}
return nil
}
// RunSSServer starts a shadowsocks server running, and returns the server or an error.
func RunSSServer(filename string, natTimeout time.Duration, sm metrics.ShadowsocksMetrics, replayHistory int) (*SSServer, error) {
server := &SSServer{
natTimeout: natTimeout,
m: sm,
replayCache: service.NewReplayCache(replayHistory),
ports: make(map[int]*ssPort),
}
err := server.loadConfig(filename)
if err != nil {
return nil, fmt.Errorf("Failed to load config file %v: %v", filename, err)
}
sigHup := make(chan os.Signal, 1)
signal.Notify(sigHup, syscall.SIGHUP)
go func() {
for range sigHup {
logger.Info("Updating config")
if err := server.loadConfig(filename); err != nil {
logger.Errorf("Could not reload config: %v", err)
}
}
}()
return server, nil
}
type Config struct {
Keys []struct {
ID string
Port int
Cipher string
Secret string
}
}
func readConfig(filename string) (*Config, error) {
config := Config{}
configData, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(configData, &config)
return &config, err
}
func main() {
var flags struct {
ConfigFile string
MetricsAddr string
IPCountryDB string
natTimeout time.Duration
replayHistory int
Verbose bool
Version bool
}
flag.StringVar(&flags.ConfigFile, "config", "", "Configuration filename")
flag.StringVar(&flags.MetricsAddr, "metrics", "", "Address for the Prometheus metrics")
flag.StringVar(&flags.IPCountryDB, "ip_country_db", "", "Path to the ip-to-country mmdb file")
flag.DurationVar(&flags.natTimeout, "udptimeout", defaultNatTimeout, "UDP tunnel timeout")
flag.IntVar(&flags.replayHistory, "replay_history", 0, "Replay buffer size (# of handshakes)")
flag.BoolVar(&flags.Verbose, "verbose", false, "Enables verbose logging output")
flag.BoolVar(&flags.Version, "version", false, "The version of the server")
flag.Parse()
if flags.Verbose {
logging.SetLevel(logging.DEBUG, "")
} else {
logging.SetLevel(logging.INFO, "")
}
if flags.Version {
fmt.Println(version)
return
}
if flags.ConfigFile == "" {
flag.Usage()
return
}
if flags.MetricsAddr != "" {
http.Handle("/metrics", promhttp.Handler())
go func() {
logger.Fatal(http.ListenAndServe(flags.MetricsAddr, nil))
}()
logger.Infof("Metrics on http://%v/metrics", flags.MetricsAddr)
}
var ipCountryDB *geoip2.Reader
var err error
if flags.IPCountryDB != "" {
logger.Infof("Using IP-Country database at %v", flags.IPCountryDB)
ipCountryDB, err = geoip2.Open(flags.IPCountryDB)
if err != nil {
log.Fatalf("Could not open geoip database at %v: %v", flags.IPCountryDB, err)
}
defer ipCountryDB.Close()
}
m := metrics.NewPrometheusShadowsocksMetrics(ipCountryDB, prometheus.DefaultRegisterer)
m.SetBuildInfo(version)
_, err = RunSSServer(flags.ConfigFile, flags.natTimeout, m, flags.replayHistory)
if err != nil {
logger.Fatal(err)
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}