-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgap.go
78 lines (66 loc) · 1.46 KB
/
gap.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path"
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
version = "0.0.1"
configDir = ".gap-proxy"
configFile = "config.json"
)
type config struct {
ServerAddr string `json:"server"`
LocalAddr string `json:"local"`
Key string `json:"key"`
}
func workDir() string {
dir, _ := homedir.Dir()
return path.Join(dir, configDir)
}
func loadConf() (conf *config, err error) {
message := "could not load config file"
var buf []byte
if buf, err = ioutil.ReadFile(path.Join(workDir(), configFile)); err != nil {
err = errors.Wrapf(err, message)
return
}
if err = json.Unmarshal(buf, &conf); err != nil {
err = errors.Wrap(err, message)
}
return
}
func main() {
log.SetFlags(0)
var root = &cobra.Command{
Use: os.Args[0],
Short: "Gap is a secure socks5 proxy to speed up your network connection.",
SilenceUsage: true,
SilenceErrors: true,
}
cobra.EnableCommandSorting = false
log.SetOutput(os.Stdout)
conf, err := loadConf()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
root.AddCommand(cmdLocal(conf))
root.AddCommand(cmdServer(conf))
root.AddCommand(cmdWnd())
root.AddCommand(cmdVersion())
var format = "%s\n"
if os.Getenv(markEnvName) == markEnvValue {
log.SetFlags(log.LstdFlags | log.Llongfile)
format = "%+v\n"
}
if err := root.Execute(); err != nil {
log.Printf(format, err)
}
}