| 1 | package mobilelink |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "net" |
| 9 | "net/http" |
| 10 | "net/http/cookiejar" |
| 11 | "net/url" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | type Workbench struct { |
| 18 | phoneRoom string |
| 19 | origin string |
| 20 | jar http.CookieJar |
| 21 | transport *http.Transport |
| 22 | } |
| 23 | |
| 24 | func loopback(host string) bool { |
| 25 | ip := net.ParseIP(host) |
| 26 | return host == "localhost" || ip != nil && ip.IsLoopback() |
| 27 | } |
| 28 | |
| 29 | func explicitPort(u *url.URL) bool { |
| 30 | port, err := strconv.Atoi(u.Port()) |
| 31 | return err == nil && port >= 1 && port <= 65535 |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
| 36 | func OpenWorkbench(ctx context.Context, origin, ticket string) (*Workbench, error) { |
| 37 | u, err := url.Parse(origin) |
| 38 | if err != nil || u.Scheme != "http" || !loopback(u.Hostname()) || !explicitPort(u) || |
| 39 | u.User != nil || u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || len(ticket) < 32 || len(ticket) > 512 { |
| 40 | return nil, errors.New("invalid local Cage entry") |
| 41 | } |
| 42 | hostname := u.Hostname() |
| 43 | if hostname == "localhost" { |
| 44 | hostname = "127.0.0.1" |
| 45 | } |
| 46 | target := net.JoinHostPort(hostname, u.Port()) |
| 47 | dialer := net.Dialer{Timeout: 5 * time.Second} |
| 48 | transport := &http.Transport{Proxy: nil, MaxIdleConnsPerHost: 8, IdleConnTimeout: time.Minute, ResponseHeaderTimeout: 35 * time.Second, |
| 49 | DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { |
| 50 | if network != "tcp" || address != u.Host { |
| 51 | return nil, errors.New("nonlocal upstream refused") |
| 52 | } |
| 53 | return dialer.DialContext(ctx, "tcp", target) |
| 54 | }, |
| 55 | } |
| 56 | jar, _ := cookiejar.New(nil) |
| 57 | b := &Workbench{origin: origin, transport: transport, jar: jar} |
| 58 | payload, err := json.Marshal(map[string]string{"token": ticket}) |
| 59 | if err != nil { |
| 60 | return nil, errors.New("invalid entry ticket") |
| 61 | } |
| 62 | defer clear(payload) |
| 63 | ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 64 | defer cancel() |
| 65 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, origin+"/api/session", bytes.NewReader(payload)) |
| 66 | if err != nil { |
| 67 | return nil, errors.New("invalid local Cage origin") |
| 68 | } |
| 69 | req.GetBody = nil |
| 70 | req.Header.Set("Content-Type", "application/json") |
| 71 | req.Header.Set("Origin", origin) |
| 72 | response, err := transport.RoundTrip(req) |
| 73 | if err != nil { |
| 74 | b.Close() |
| 75 | return nil, errors.New("Cage entry ticket exchange failed") |
| 76 | } |
| 77 | response.Body.Close() |
| 78 | if response.StatusCode == http.StatusOK { |
| 79 | for _, cookie := range response.Cookies() { |
| 80 | if cookie.Name == "cage_session_"+u.Port() && cookie.Value != "" { |
| 81 | jar.SetCookies(u, []*http.Cookie{cookie}) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if len(jar.Cookies(u)) != 1 { |
| 86 | b.Close() |
| 87 | return nil, errors.New("Cage did not issue a valid local session cookie") |
| 88 | } |
| 89 | return b, nil |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | func (b *Workbench) SetPhoneRoom(proof string) { b.phoneRoom = proof } |
| 95 | |
| 96 | const PhoneRoomHeader = "X-Cage-Phone-Room" |
| 97 | |
| 98 | func (b *Workbench) Close() { b.transport.CloseIdleConnections() } |
| 99 | |
| 100 | |
| 101 | |
| 102 | type localTransport struct{ b *Workbench } |
| 103 | |
| 104 | func (t localTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 105 | if t.b.phoneRoom != "" && strings.HasPrefix(req.URL.Path, "/api/mobile/") { |
| 106 | req.Header.Set(PhoneRoomHeader, t.b.phoneRoom) |
| 107 | } |
| 108 | for _, cookie := range t.b.jar.Cookies(req.URL) { |
| 109 | req.AddCookie(cookie) |
| 110 | } |
| 111 | return t.b.transport.RoundTrip(req) |
| 112 | } |
| 113 | |
| 114 | func (b *Workbench) handler() http.Handler { |
| 115 | return operations(func(ctx context.Context, method, path string, body []byte) (*Reply, error) { |
| 116 | return request(ctx, localTransport{b}, b.origin, method, path, body, true) |
| 117 | }) |
| 118 | } |