| 55 | } |
| 56 | |
| 57 | func New(conf *config.Config, version, pubKey, privKey string) *Proxy { |
| 58 | k := kite.New("tunnelproxy", version) |
| 59 | k.Config = conf |
| 60 | |
| 61 | // Listen on 3999 by default |
| 62 | if k.Config.Port == 0 { |
| 63 | k.Config.Port = DefaultPort |
| 64 | } |
| 65 | |
| 66 | p := &Proxy{ |
| 67 | Kite: k, |
| 68 | readyC: make(chan bool), |
| 69 | closeC: make(chan bool), |
| 70 | pubKey: pubKey, |
| 71 | privKey: privKey, |
| 72 | kites: make(map[string]*PrivateKite), |
| 73 | mux: http.NewServeMux(), |
| 74 | RegisterToKontrol: true, |
| 75 | PublicHost: DefaultPublicHost, |
| 76 | } |
| 77 | |
| 78 | p.Kite.HandleFunc("register", p.handleRegister) |
| 79 | |
| 80 | p.mux.Handle("/", p.Kite) |
| 81 | p.mux.Handle("/proxy/", sockjsHandlerWithRequest("/proxy", sockjs.DefaultOptions, p.handleProxy)) // Handler for clients outside |
| 82 | p.mux.Handle("/tunnel/", sockjsHandlerWithRequest("/tunnel", sockjs.DefaultOptions, p.handleTunnel)) // Handler for kites behind |
| 83 | |
| 84 | // Remove URL from the map when PrivateKite disconnects. |
| 85 | k.OnDisconnect(func(r *kite.Client) { |
| 86 | delete(p.kites, r.Kite.ID) |
| 87 | }) |
| 88 | |
| 89 | return p |
| 90 | } |
| 91 | |
| 92 | // sockjsHandlerWithRequest is a wrapper around the sockjs.Handler that |
| 93 | // includes a *http.Request context. |