-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (135 loc) · 3.5 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"fmt"
"strings"
quicgo "github.com/quic-go/quic-go"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/wikylyu/mtop/config"
"github.com/wikylyu/mtop/db"
"github.com/wikylyu/mtop/tunnel"
"github.com/wikylyu/mtop/tunnel/protocol/quic"
)
const AppName = "mtop"
const AppVersion = "0.0.2"
var AppCommit = ""
var rootCmd = &cobra.Command{
Use: AppName,
Short: AppName + " is a network proxy server",
Run: func(cmd *cobra.Command, args []string) {
stype, listen, config := initServerConfig()
if stype == "quic" {
runQUICServer(listen, config)
} else {
runTLSServer(listen, config)
}
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: fmt.Sprintf("show %s's version", AppName),
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s version: %s\n", AppName, AppVersion)
fmt.Printf("git commit: %s\n", AppCommit)
},
}
var cfgFile string
func init() {
cobra.OnInitialize(initMTop)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", fmt.Sprintf("config file (default is %s/etc/%s/%s.yaml)", config.PREFIX, AppName, AppName))
rootCmd.AddCommand(versionCmd)
}
func initMTop() {
config.Init(AppName, cfgFile)
config.InitLog()
initDatabase()
}
func initDatabase() {
var cfg struct {
Debug bool `json:"debug" yaml:"debug"`
DriverName string `json:"driverName" yaml:"driverName"`
DSN string `json:"dsn" yaml:"dsn"`
}
if err := config.Unmarshal("db", &cfg); err != nil {
panic(err)
}
if err := db.Init(cfg.DriverName, cfg.DSN, cfg.Debug); err != nil {
panic(err)
}
}
func initServerConfig() (string, string, *tls.Config) {
var cfg struct {
Type string `json:"type" yaml:"type"`
Listen string `json:"listen" yaml:"listen"`
CRT string `json:"crt" yaml:"crt"`
Key string `json:"key" yaml:"key"`
Proto string `json:"proto" yaml:"proto"`
}
if err := config.Unmarshal("server", &cfg); err != nil {
panic(err)
}
if cfg.CRT == "" || cfg.Key == "" {
log.Fatalf("certificate key not configured")
}
cer, err := tls.LoadX509KeyPair(cfg.CRT, cfg.Key)
if err != nil {
log.Fatalf("load x509 key error: %v", err)
}
if cfg.Proto == "" {
cfg.Proto = "mtop"
}
return strings.ToLower(cfg.Type), cfg.Listen, &tls.Config{
Certificates: []tls.Certificate{cer},
NextProtos: []string{cfg.Proto},
}
}
func handleQUICConn(conn quicgo.Connection) {
defer conn.CloseWithError(0, "")
for {
stream, err := conn.AcceptStream(context.Background())
if err != nil {
log.Debugf("[QUIC] accept stream error:%v", err)
break
}
t := tunnel.NewTunnel(quic.NewConn(stream, conn.RemoteAddr()))
go t.Run()
}
}
func runQUICServer(listen string, config *tls.Config) {
listener, err := quicgo.ListenAddr(listen, config, nil)
if err != nil {
log.Fatalf("listen on %s error: %v", listen, err)
}
log.Infof("[QUIC] listen on %s", listen)
defer listener.Close()
for {
conn, err := listener.Accept(context.Background())
if err != nil {
log.Errorf("[QUIC] accept error:%v", err)
continue
}
go handleQUICConn(conn)
}
}
func runTLSServer(listen string, config *tls.Config) {
listener, err := tls.Listen("tcp", listen, config)
if err != nil {
log.Fatalf("[TLS] listen on %s error: %v", listen, err)
}
log.Infof("[TLS] listen on %s", listen)
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
log.Warnf("[TLS] accept error: %v", err)
continue
}
t := tunnel.NewTunnel(conn)
go t.Run()
}
}
func main() {
rootCmd.Execute()
}