102 lines
3.4 KiB
Go
102 lines
3.4 KiB
Go
package routers
|
|
|
|
// Browser forwarding is scoped to a freshly observed Ethernet router. It has
|
|
// no arbitrary destination, credential injection, redirects or host route edits.
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type AdminRequest struct {
|
|
BindingID string `json:"binding_id"`
|
|
DeviceID string `json:"device_id"`
|
|
Method string `json:"method"`
|
|
Path string `json:"path"`
|
|
Headers map[string]string `json:"headers"`
|
|
Body []byte `json:"body"`
|
|
}
|
|
type AdminResponse struct {
|
|
Status int `json:"status"`
|
|
Headers map[string][]string `json:"headers"`
|
|
Body []byte `json:"body"`
|
|
}
|
|
|
|
func (r AdminRequest) Valid() bool {
|
|
u, e := url.ParseRequestURI(r.Path)
|
|
return e == nil && strings.HasPrefix(r.Path, "/") && !strings.HasPrefix(r.Path, "//") && u.Host == "" && u.Scheme == "" && !strings.ContainsAny(r.Path, "\r\n\\") && len(r.Path) <= 8192 && len(r.Body) <= 1048576 && len(r.Headers) <= 8 && (r.Method == "GET" || r.Method == "POST" || r.Method == "HEAD")
|
|
}
|
|
func (c *Collector) Admin(ctx context.Context, in AdminRequest) (AdminResponse, error) {
|
|
if !in.Valid() {
|
|
return AdminResponse{}, errProtocol
|
|
}
|
|
var chosen *Device
|
|
for _, d := range c.Snapshot().Items {
|
|
if d.ID == in.DeviceID && d.Attached && d.Connection == "ethernet" && d.State == "ready" && time.Now().Unix()-d.ObservedAt < 40 {
|
|
copy := d
|
|
chosen = ©
|
|
break
|
|
}
|
|
}
|
|
if chosen == nil {
|
|
return AdminResponse{}, errors.New("router not available")
|
|
}
|
|
d := chosen
|
|
candidate := Candidate{Address: d.Address, Interface: d.Interface, LocalIP: d.LocalIP, MAC: c.neighbor(d.Interface, d.Address)}
|
|
if candidate.MAC == "" {
|
|
return AdminResponse{}, errLink
|
|
}
|
|
a := newAPI(candidate)
|
|
a.client.Jar = nil
|
|
a.client.Timeout = 8 * time.Second
|
|
if in.Method == "POST" {
|
|
// Native mode changes and diagnostics synchronously wait for the router.
|
|
a.client.Timeout = 90 * time.Second
|
|
}
|
|
a.client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
|
defer a.client.CloseIdleConnections()
|
|
req, e := http.NewRequestWithContext(ctx, in.Method, "http://"+d.Address+in.Path, bytes.NewReader(in.Body))
|
|
if e != nil {
|
|
return AdminResponse{}, e
|
|
}
|
|
for _, key := range []string{"Cookie", "Content-Type", "Accept", "Accept-Encoding"} {
|
|
if v := in.Headers[key]; len(v) <= 16384 {
|
|
req.Header.Set(key, v)
|
|
}
|
|
}
|
|
req.Header.Set("Origin", "http://"+d.Address)
|
|
req.Header.Set("Referer", "http://"+d.Address+"/cgi-bin/luci/dexaflow")
|
|
if !a.check() {
|
|
return AdminResponse{}, errLink
|
|
}
|
|
response, e := a.client.Do(req)
|
|
if e != nil {
|
|
return AdminResponse{}, e
|
|
}
|
|
defer response.Body.Close()
|
|
body, e := io.ReadAll(io.LimitReader(response.Body, 8*1048576+1))
|
|
if e != nil || len(body) > 8*1048576 {
|
|
return AdminResponse{}, errProtocol
|
|
}
|
|
out := AdminResponse{Status: response.StatusCode, Body: body, Headers: map[string][]string{}}
|
|
for _, key := range []string{"Content-Type", "Content-Encoding", "Set-Cookie", "Location", "Content-Disposition"} {
|
|
values := response.Header.Values(key)
|
|
if key == "Location" && len(values) > 0 {
|
|
u, e := url.Parse(values[0])
|
|
if e != nil || (u.Host != "" && u.Host != d.Address) {
|
|
return AdminResponse{}, errProtocol
|
|
}
|
|
values = []string{u.RequestURI()}
|
|
}
|
|
if len(values) > 0 {
|
|
out.Headers[key] = values
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|