-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (119 loc) · 2.63 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
// +build linux darwin
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"reflect"
"runtime"
"time"
"github.com/getlantern/systray"
//protocols
_ "github.com/imgk/shadow/proto/register"
"github.com/imgk/shadow/app"
)
func main() {
type FlagConfig struct {
Verbose bool
FilePath string
Timeout time.Duration
}
conf := FlagConfig{}
flag.BoolVar(&conf.Verbose, "v", false, "enable verbose mode")
flag.StringVar(&conf.FilePath, "c", "", "config file")
flag.DurationVar(&conf.Timeout, "t", time.Minute*3, "timeout")
flag.Parse()
if conf.FilePath == "" {
dir, err := os.UserHomeDir()
if err != nil {
log.Panic(err)
}
conf.FilePath = filepath.Join(dir, ".config", "shadow", "config.json")
}
w := io.Writer(nil)
if conf.Verbose {
w = os.Stdout
}
app, err := app.NewApp(conf.FilePath, conf.Timeout, w)
if err != nil {
log.Panic(err)
}
if err := app.Run(); err != nil {
log.Panic(err)
}
fmt.Println("shadow - a transparent proxy for Windows, Linux and macOS")
fmt.Println("shadow is running...")
systray.Run(func() {
systray.SetIcon(icon)
systray.SetTitle("")
systray.SetTooltip("Shadow")
items := make([]item, 0, 2)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
items = append(items, item{
condition: reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(sigCh),
},
function: systray.Quit,
terminate: true,
})
mQuit := systray.AddMenuItem("Exit", "Quit Shadow")
items = append(items, item{
condition: reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(mQuit.ClickedCh),
},
function: systray.Quit,
terminate: true,
})
go run(items)
}, func() { app.Close() })
fmt.Println("shadow is closing...")
select {
case <-time.After(time.Second * 10):
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
lines := bytes.Split(buf, []byte{'\n'})
fmt.Println("Failed to shutdown after 10 seconds. Probably dead locked. Printing stack and killing.")
for _, line := range lines {
if len(bytes.TrimSpace(line)) > 0 {
fmt.Println(string(line))
}
}
os.Exit(777)
case <-app.Done():
}
}
type item struct {
condition reflect.SelectCase
function func()
terminate bool
}
func run(items []item) {
cases := make([]reflect.SelectCase, len(items))
for i, _ := range items {
cases[i] = items[i].condition
}
for {
i, _, _ := reflect.Select(cases)
if item := items[i]; item.terminate {
item.function()
return
} else {
item.function()
}
}
}