1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/app/browserforwarder/forwarder.go

130 lines
3.3 KiB
Go
Raw Normal View History

2021-03-16 19:19:00 -04:00
package browserforwarder
import (
"bytes"
"context"
2021-03-27 20:44:04 -04:00
"io"
"net/http"
"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
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/platform/securedload"
"github.com/v2fly/v2ray-core/v5/features/extension"
"github.com/v2fly/v2ray-core/v5/transport/internet"
2021-03-16 19:19:00 -04:00
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2021-03-16 19:19:00 -04:00
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 {
if k == "Sec-Websocket-Protocol" {
2021-05-01 10:50:20 -04:00
protocolHeader = true
protocolHeaderValue = v[0]
} else {
unsupportedHeader = true
}
}
if unsupportedHeader {
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 {
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 {
return newError("forwarder cannot listen on the port ", f.config.ListenPort).Base(err)
2021-03-16 19:19:00 -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"
}
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
}))
}