1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

added forwarder service

This commit is contained in:
Shelikhoo 2021-03-16 23:19:00 +00:00
parent 2d778239a3
commit f63ee69fcf
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
5 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,14 @@
syntax = "proto3";
package v2ray.core.app.browserforwarder;
option csharp_namespace = "V2Ray.Core.App.Browserforwarder";
option go_package = "github.com/v2fly/v2ray-core/v4/app/browserforwarder";
option java_package = "com.v2ray.core.app.browserforwarder";
option java_multiple_files = true;
// Config is the settings for BrowserForwarder.
message Config {
string listen_addr = 1;
int32 listen_port = 2;
}

View File

@ -0,0 +1,94 @@
package browserforwarder
import (
"bytes"
"context"
"github.com/v2fly/BrowserBridge/handler"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/platform/securedload"
"github.com/v2fly/v2ray-core/v4/features/ext"
"github.com/v2fly/v2ray-core/v4/transport/internet"
"io"
"net/http"
"time"
)
//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)
break
case "link":
f.forwarder.ServeBridge(writer, request)
}
}
func (f *Forwarder) DialWebsocket(url string, header http.Header) (io.ReadWriteCloser, error) {
return f.forwarder.Dial(url)
}
func (f *Forwarder) Type() interface{} {
return ext.BrowserForwarderType()
}
func (f *Forwarder) Start() error {
f.forwarder = handler.NewHttpHandle()
f.httpserver = &http.Server{Handler: f}
address := net.ParseAddress(f.config.ListenAddr)
listener, err := internet.ListenSystem(f.ctx, &net.TCPAddr{IP: address.IP(), Port: int(f.config.ListenPort)}, nil)
if err != nil {
return newError("forwarder cannot listen on the port").Base(err)
}
go func() {
err = f.httpserver.Serve(listener)
if err != nil {
newError("cannot serve http forward server").Base(err).WriteToLog()
}
}()
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
data, err := securedload.GetAssetSecured("browserforwarder/" + content)
if err != nil {
err = newError("cannot load necessary resources").Base(err)
http.Error(rw, err.Error(), 403)
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
}))
}

View File

@ -1,6 +1,11 @@
package securedload package securedload
import "path/filepath"
func GetAssetSecured(name string) ([]byte, error) { func GetAssetSecured(name string) ([]byte, error) {
name = filepath.FromSlash(name)
var err error var err error
for k, v := range knownProtectedLoader { for k, v := range knownProtectedLoader {
if loadedData, errLoad := v.VerifyAndLoad(name); errLoad == nil { if loadedData, errLoad := v.VerifyAndLoad(name); errLoad == nil {

View File

@ -8,3 +8,7 @@ import (
type BrowserForwarder interface { type BrowserForwarder interface {
DialWebsocket(url string, header http.Header) (io.ReadWriteCloser, error) DialWebsocket(url string, header http.Header) (io.ReadWriteCloser, error)
} }
func BrowserForwarderType() interface{} {
return (*BrowserForwarder)(nil)
}

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/pires/go-proxyproto v0.5.0 github.com/pires/go-proxyproto v0.5.0
github.com/seiflotfy/cuckoofilter v0.0.0-20201222105146-bc6005554a0c github.com/seiflotfy/cuckoofilter v0.0.0-20201222105146-bc6005554a0c
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/v2fly/BrowserBridge v0.0.0-20210316223221-b4ab84595cdc // indirect
github.com/v2fly/VSign v0.0.0-20201108000810-e2adc24bf848 github.com/v2fly/VSign v0.0.0-20201108000810-e2adc24bf848
go.starlark.net v0.0.0-20210305151048-6a590ae7f4eb go.starlark.net v0.0.0-20210305151048-6a590ae7f4eb
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83