-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.go
108 lines (90 loc) · 2.41 KB
/
setup.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
package torproxy
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func init() {
caddy.RegisterModule(TorProxy{})
httpcaddyfile.RegisterHandlerDirective("torproxy", parse)
}
func parse(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var t TorProxy
err := t.UnmarshalCaddyfile(h.Dispenser)
return t, err
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (t *TorProxy) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
var config Config
var client Tor
to := make(map[string]string)
config_handler:
for d.Next() {
if d.Val() == "torproxy" {
d.Next() // skip directive name
}
// Parse the Tor client's config
if d.Val() == "{" {
for d.Next() {
if d.Val() == "}" {
continue config_handler
}
if err := client.ParseTor(d); err != nil {
return fmt.Errorf("Couldn't parse the Tor client config: %s", err.Error())
}
}
}
// Parse the Config.From and Config.To URIs
fromURI, err := url.Parse(d.Val())
if err != nil {
return fmt.Errorf("Couldn't parse the `from` URI %s", err.Error())
}
toURI, err := url.Parse(d.RemainingArgs()[0])
if err != nil {
return fmt.Errorf("Couldn't parse the `from` URI: %s", err.Error())
}
if toURI.Scheme == "" {
toURI.Scheme = "http"
}
// Fill the config instance
to[fromURI.String()] = toURI.String()
}
config.To = to
config.Client = &client
config.Client.SetDefaults()
t.Config = config
if t.Config.Client.Host == "" && !t.testing {
t.Config.Client.Start()
}
return nil
}
// CaddyModule returns the Caddy module information.
func (TorProxy) CaddyModule() caddy.ModuleInfo {
t := &TorProxy{Config: Config{Client: &Tor{}}}
pool := caddy.NewUsagePool()
pool.LoadOrNew("torclient", TorConstructor)
if t.Config.Client.Host == "" && !t.testing {
if err := t.Config.Client.IsInstalled(); err != nil {
log.Fatalf(err.Error())
}
}
return caddy.ModuleInfo{
Name: "http.handlers.torproxy",
New: func() caddy.Module {
return t
},
}
}
type TorProxy struct {
Config Config
// Set "testing" to true in tests to skip Tor client's auto start
testing bool
}
func (t TorProxy) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
return t.Config.Proxy(w, r)
}