2021-03-16 19:19:00 -04:00
|
|
|
package browserforwarder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-03-27 20:44:04 -04:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-05-01 08:50:30 -04:00
|
|
|
"strings"
|
2021-03-27 20:44:04 -04:00
|
|
|
"time"
|
|
|
|
|
2021-03-16 19:19:00 -04:00
|
|
|
"github.com/v2fly/BrowserBridge/handler"
|
2021-03-27 20:44:04 -04:00
|
|
|
|
2021-03-16 19:19:00 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/securedload"
|
2021-04-30 23:15:39 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/features/extension"
|
2021-03-16 19:19:00 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
|
|
|
|
|
|
|
|
type Forwarder struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
forwarder *handler.HTTPHandle
|
|
|
|
httpserver *http.Server
|
|
|
|
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Forwarder) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
requestPath := request.URL.Path[1:]
|
|
|
|
|
|
|
|
switch requestPath {
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case "index.js":
|
|
|
|
BridgeResource(writer, request, requestPath)
|
|
|
|
case "link":
|
|
|
|
f.forwarder.ServeBridge(writer, request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Forwarder) DialWebsocket(url string, header http.Header) (io.ReadWriteCloser, error) {
|
2021-05-01 10:50:20 -04:00
|
|
|
protocolHeader := false
|
|
|
|
protocolHeaderValue := ""
|
|
|
|
unsupportedHeader := false
|
|
|
|
for k, v := range header {
|
2021-05-01 11:07:03 -04:00
|
|
|
if k == "Sec-Websocket-Protocol" {
|
2021-05-01 10:50:20 -04:00
|
|
|
protocolHeader = true
|
|
|
|
protocolHeaderValue = v[0]
|
|
|
|
} else {
|
|
|
|
unsupportedHeader = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if unsupportedHeader {
|
2021-09-05 05:31:40 -04:00
|
|
|
return nil, newError("unsupported header used, only Sec-Websocket-Protocol is supported for forwarder")
|
2021-05-01 10:50:20 -04:00
|
|
|
}
|
|
|
|
if !protocolHeader {
|
|
|
|
return f.forwarder.Dial(url)
|
|
|
|
}
|
|
|
|
return f.forwarder.Dial2(url, protocolHeaderValue)
|
2021-03-16 19:19:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Forwarder) Type() interface{} {
|
2021-04-30 19:06:10 -04:00
|
|
|
return extension.BrowserForwarderType()
|
2021-03-16 19:19:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Forwarder) Start() error {
|
2021-05-01 08:50:30 -04:00
|
|
|
if f.config.ListenAddr != "" {
|
|
|
|
f.forwarder = handler.NewHttpHandle()
|
|
|
|
f.httpserver = &http.Server{Handler: f}
|
|
|
|
|
|
|
|
var listener net.Listener
|
|
|
|
var err error
|
|
|
|
address := net.ParseAddress(f.config.ListenAddr)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case address.Family().IsIP():
|
|
|
|
listener, err = internet.ListenSystem(f.ctx, &net.TCPAddr{IP: address.IP(), Port: int(f.config.ListenPort)}, nil)
|
|
|
|
case strings.EqualFold(address.Domain(), "localhost"):
|
|
|
|
listener, err = internet.ListenSystem(f.ctx, &net.TCPAddr{IP: net.IP{127, 0, 0, 1}, Port: int(f.config.ListenPort)}, nil)
|
|
|
|
default:
|
|
|
|
return newError("forwarder cannot listen on the address: ", address)
|
|
|
|
}
|
2021-03-16 19:19:00 -04:00
|
|
|
if err != nil {
|
2021-05-01 08:50:30 -04:00
|
|
|
return newError("forwarder cannot listen on the port ", f.config.ListenPort).Base(err)
|
2021-03-16 19:19:00 -04:00
|
|
|
}
|
2021-05-01 08:50:30 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := f.httpserver.Serve(listener); err != nil {
|
|
|
|
newError("cannot serve http forward server").Base(err).WriteToLog()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-03-16 19:19:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Forwarder) Close() error {
|
|
|
|
if f.httpserver != nil {
|
|
|
|
return f.httpserver.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BridgeResource(rw http.ResponseWriter, r *http.Request, path string) {
|
|
|
|
content := path
|
2021-03-20 17:37:06 -04:00
|
|
|
if content == "" {
|
|
|
|
content = "index.html"
|
|
|
|
}
|
2021-04-05 12:54:53 -04:00
|
|
|
data, err := securedload.GetAssetSecured("browserforwarder/" + content)
|
2021-03-16 19:19:00 -04:00
|
|
|
if err != nil {
|
|
|
|
err = newError("cannot load necessary resources").Base(err)
|
2021-03-27 21:33:00 -04:00
|
|
|
http.Error(rw, err.Error(), http.StatusForbidden)
|
2021-03-16 19:19:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(rw, r, path, time.Now(), bytes.NewReader(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewForwarder(ctx context.Context, cfg *Config) *Forwarder {
|
|
|
|
return &Forwarder{config: cfg, ctx: ctx}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
|
|
|
return NewForwarder(ctx, cfg.(*Config)), nil
|
|
|
|
}))
|
|
|
|
}
|