@@ -60,10 +60,48 @@ func (t *Tor) Serve() error {
60
60
// Start tor with some defaults + elevated verbosity
61
61
logger .Info ("starting and registering onion service, please wait a bit..." )
62
62
63
- if _ , err := exec .LookPath ("tor" ); err != nil {
64
- logger .Fatal ("%v" , err )
63
+ handler := newWeb ().handle (t .pool )
64
+ server := & http.Server {
65
+ ReadTimeout : 5 * time .Minute ,
66
+ WriteTimeout : 5 * time .Minute ,
67
+ IdleTimeout : 5 * time .Minute ,
68
+ Handler : handler ,
69
+ }
70
+
71
+ switch {
72
+ case torExist ():
73
+ logger .Info ("start a tor hidden server" )
74
+ t .startTorServer (server )
75
+ default :
76
+ logger .Info ("start a clear web server" )
77
+ server .Addr = config .Opts .ListenAddr ()
78
+ startHTTPServer (server )
79
+ }
80
+
81
+ <- t .ctx .Done ()
82
+ logger .Info ("stopping tor hidden service..." )
83
+ if err := server .Shutdown (t .ctx ); err != nil {
84
+ logger .Error ("shutdown tor hidden service failed: %v" , err )
85
+ return err
86
+ }
87
+
88
+ return errors .New ("done" )
89
+ }
90
+
91
+ func (t * Tor ) torrc () string {
92
+ if config .Opts .TorrcFile () == "" {
93
+ return ""
94
+ }
95
+ if torPortBusy () {
96
+ return ""
65
97
}
98
+ if _ , err := os .Open (config .Opts .TorrcFile ()); err != nil {
99
+ return ""
100
+ }
101
+ return config .Opts .TorrcFile ()
102
+ }
66
103
104
+ func (t * Tor ) startTorServer (server * http.Server ) {
67
105
var pvk ed25519.PrivateKey
68
106
if config .Opts .TorPrivKey () == "" {
69
107
if keypair , err := ed25519 .GenerateKey (rand .Reader ); err != nil {
@@ -92,7 +130,6 @@ func (t *Tor) Serve() error {
92
130
if err != nil {
93
131
logger .Fatal ("failed to start tor: %v" , err )
94
132
}
95
- defer e .Close ()
96
133
e .DeleteDataDirOnClose = true
97
134
e .StopProcessOnClose = false
98
135
@@ -102,40 +139,31 @@ func (t *Tor) Serve() error {
102
139
if err != nil {
103
140
logger .Fatal ("failed to create onion service: %v" , err )
104
141
}
105
- defer onion .Close ()
106
142
onion .CloseLocalListenerOnClose = false
107
143
108
144
logger .Info (`listening on "%s" without TLS` , color .BlueString (onion .LocalListener .Addr ().String ()))
109
145
logger .Info ("please open a Tor capable browser and navigate to http://%v.onion" , onion .ID )
110
146
111
- server := http.Server {Handler : newWeb ().handle (t .pool )}
112
147
go func () {
113
148
if err := server .Serve (onion ); err != nil {
114
- logger .Error ("serve tor hidden service failed: %v" , err )
149
+ logger .Fatal ("serve tor hidden service failed: %v" , err )
115
150
}
116
- }()
117
151
118
- <- t .ctx .Done ()
119
- logger .Info ("stopping tor hidden service..." )
120
- if err := server .Shutdown (t .ctx ); err != nil {
121
- logger .Error ("shutdown tor hidden service failed: %v" , err )
122
- return err
123
- }
124
-
125
- return errors .New ("done" )
152
+ // Close onion service.
153
+ // TODO
154
+ <- t .ctx .Done ()
155
+ e .Close ()
156
+ onion .Close ()
157
+ }()
126
158
}
127
159
128
- func (t * Tor ) torrc () string {
129
- if config .Opts .TorrcFile () == "" {
130
- return ""
131
- }
132
- if torPortBusy () {
133
- return ""
134
- }
135
- if _ , err := os .Open (config .Opts .TorrcFile ()); err != nil {
136
- return ""
137
- }
138
- return config .Opts .TorrcFile ()
160
+ func startHTTPServer (server * http.Server ) {
161
+ go func () {
162
+ logger .Info (`Listening on "%s" without TLS` , color .BlueString (server .Addr ))
163
+ if err := server .ListenAndServe (); err != http .ErrServerClosed {
164
+ logger .Fatal ("Server failed to start: %v" , err )
165
+ }
166
+ }()
139
167
}
140
168
141
169
func torPortBusy () bool {
@@ -153,3 +181,10 @@ func torPortBusy() bool {
153
181
154
182
return false
155
183
}
184
+
185
+ func torExist () bool {
186
+ if _ , err := exec .LookPath ("tor" ); err != nil {
187
+ return false
188
+ }
189
+ return true
190
+ }
0 commit comments