-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
executable file
·58 lines (49 loc) · 1.11 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
//SERVER
package main
import (
"fmt"
"log"
"net"
"./cmd"
"./core"
"./server"
"github.com/phayes/freeport"
)
var version = "master"
func main() {
fmt.Print("Ich*Liebe*Dich~ \n", "SERVER\n")
log.SetFlags(log.Lshortfile)
// 服务端监听端口随机生成
port, err := freeport.GetFreePort()
if err != nil {
// 随机端口失败就采用 7448
port = 7448
}
// 默认配置
config := &cmd.Config{
ListenAddr: fmt.Sprintf(":%d", port),
// 密码随机生成
Password: core.RandPassword().String(),
}
config.ReadConfig()
// 解析配置
password, err := core.ParsePassword(config.Password)
if err != nil {
log.Fatalln(err)
}
listenAddr, err := net.ResolveTCPAddr("tcp", config.ListenAddr)
if err != nil {
log.Fatalln(err)
}
// 启动 server 端并监听
lsServer := server.New(password, listenAddr)
log.Fatalln(lsServer.Listen(func(listenAddr net.Addr) {
log.Println("使用配置:", fmt.Sprintf(`
本地监听地址 listen:
%s
密码 password:
%s
`, listenAddr, password))
log.Printf("lightsocks-server:%s 启动成功 监听在 %s\n", version, listenAddr.String())
}))
}