forked from Psiphon-Labs/psiphon-tunnel-core
/
psiphonClient.go
152 lines (127 loc) · 4.35 KB
/
psiphonClient.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
/*
* Copyright (c) 2015, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime/pprof"
"sync"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
)
func main() {
// Define command-line parameters
var configFilename string
flag.StringVar(&configFilename, "config", "", "configuration input file")
var embeddedServerEntryListFilename string
flag.StringVar(&embeddedServerEntryListFilename, "serverList", "", "embedded server entry list input file")
var formatNotices bool
flag.BoolVar(&formatNotices, "formatNotices", false, "emit notices in human-readable format")
var profileFilename string
flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
flag.Parse()
// Handle required config file parameter
if configFilename == "" {
log.Fatalf("configuration file is required")
}
configFileContents, err := ioutil.ReadFile(configFilename)
if err != nil {
log.Fatalf("error loading configuration file: %s", err)
}
config, err := psiphon.LoadConfig(configFileContents)
if err != nil {
log.Fatalf("error processing configuration file: %s", err)
}
// Initialize notice output; use logfile, if configured
var noticeWriter io.Writer
noticeWriter = os.Stderr
if config.LogFilename != "" {
logFile, err := os.OpenFile(config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Fatalf("error opening log file: %s", err)
}
defer logFile.Close()
}
if formatNotices {
noticeWriter = psiphon.NewNoticeConsoleRewriter(noticeWriter)
}
psiphon.SetNoticeOutput(noticeWriter)
// Handle optional profiling parameter
if profileFilename != "" {
profileFile, err := os.Create(profileFilename)
if err != nil {
log.Fatalf("error opening profile file: %s", err)
}
pprof.StartCPUProfile(profileFile)
defer pprof.StopCPUProfile()
}
// Initialize data store
err = psiphon.InitDataStore(config)
if err != nil {
log.Fatalf("error initializing datastore: %s", err)
}
// Handle optional embedded server list file parameter
// If specified, the embedded server list is loaded and stored before
// running Psiphon.
if embeddedServerEntryListFilename != "" {
serverEntryList, err := ioutil.ReadFile(embeddedServerEntryListFilename)
if err != nil {
log.Fatalf("error loading embedded server entry list file: %s", err)
}
// TODO: stream embedded server list data? also, the cast makaes an unnecessary copy of a large buffer?
serverEntries, err := psiphon.DecodeAndValidateServerEntryList(string(serverEntryList))
if err != nil {
log.Fatalf("error decoding embedded server entry list file: %s", err)
}
// Since embedded server list entries may become stale, they will not
// overwrite existing stored entries for the same server.
err = psiphon.StoreServerEntries(serverEntries, false)
if err != nil {
log.Fatalf("error storing embedded server entry list data: %s", err)
}
}
// Run Psiphon
controller, err := psiphon.NewController(config)
if err != nil {
log.Fatalf("error creating controller: %s", err)
}
controllerStopSignal := make(chan struct{}, 1)
shutdownBroadcast := make(chan struct{})
controllerWaitGroup := new(sync.WaitGroup)
controllerWaitGroup.Add(1)
go func() {
defer controllerWaitGroup.Done()
controller.Run(shutdownBroadcast)
controllerStopSignal <- *new(struct{})
}()
// Wait for an OS signal or a Run stop signal, then stop Psiphon and exit
systemStopSignal := make(chan os.Signal, 1)
signal.Notify(systemStopSignal, os.Interrupt, os.Kill)
select {
case <-systemStopSignal:
psiphon.NoticeInfo("shutdown by system")
close(shutdownBroadcast)
controllerWaitGroup.Wait()
case <-controllerStopSignal:
psiphon.NoticeInfo("shutdown by controller")
}
}