-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
100 lines (87 loc) · 2.11 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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"github.com/ginuerzh/gost"
"github.com/golang/glog"
"golang.org/x/net/http2"
"io/ioutil"
"os"
"runtime"
)
var (
options struct {
ChainNodes, ServeNodes flagStringList
}
)
func init() {
var (
configureFile string
printVersion bool
)
flag.StringVar(&configureFile, "C", "", "configure file")
flag.Var(&options.ChainNodes, "F", "forward address, can make a forward chain")
flag.Var(&options.ServeNodes, "L", "listen address, can listen on multiple ports")
flag.BoolVar(&printVersion, "V", false, "print version")
flag.Parse()
fmt.Fprintf(os.Stderr, "%s\n", &options.ChainNodes)
if err := loadConfigureFile(configureFile); err != nil {
fmt.Fprintf(os.Stdout, "no configure file\n")
glog.Fatal(err)
}
if glog.V(5) {
http2.VerboseLogs = true
}
if flag.NFlag() == 0 {
flag.PrintDefaults()
fmt.Fprintf(os.Stdout, "no parameters\n")
return
}
if printVersion {
fmt.Fprintf(os.Stderr, "GOST %s (%s)\n", gost.Version, runtime.Version())
return
}
fmt.Fprintf(os.Stdout, "init finish\n")
}
func main() {
chain := gost.NewProxyChain()
if err := chain.AddProxyNodeString(options.ChainNodes...); err != nil {
glog.Fatal(err)
}
chain.Init()
serverNode, err := gost.ParseProxyNode(options.ServeNodes[0])
if err != nil {
glog.Fatal(err)
}
fmt.Fprintf(os.Stdout, "old bind address: ", serverNode)
host := os.Getenv("HOST")
port := os.Getenv("PORT")
bind := fmt.Sprintf("%s:%s", host, port)
fmt.Fprintf(os.Stdout, "\nbind address: %s\n", bind)
serverNode.Addr = bind
server := gost.NewProxyServer(serverNode, chain, &tls.Config{})
glog.Fatal(server.Serve())
}
func loadConfigureFile(configureFile string) error {
if configureFile == "" {
return nil
}
content, err := ioutil.ReadFile(configureFile)
if err != nil {
return err
}
if err := json.Unmarshal(content, &options); err != nil {
return err
}
return nil
}
type flagStringList []string
func (this *flagStringList) String() string {
return fmt.Sprintf("%s", *this)
}
func (this *flagStringList) Set(value string) error {
*this = append(*this, value)
return nil
}