1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

Fix: browserforwarder panics with empty config (#954)

Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
This commit is contained in:
秋のかえで 2021-05-01 20:50:30 +08:00 committed by GitHub
parent a585ca28a8
commit b68411f460
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import (
"context"
"io"
"net/http"
"strings"
"time"
"github.com/v2fly/BrowserBridge/handler"
@ -51,19 +52,32 @@ func (f *Forwarder) Type() interface{} {
}
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)
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)
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)
}
go func() {
err = f.httpserver.Serve(listener)
if err != nil {
return newError("forwarder cannot listen on the port ", f.config.ListenPort).Base(err)
}
go func() {
if err := f.httpserver.Serve(listener); err != nil {
newError("cannot serve http forward server").Base(err).WriteToLog()
}
}()
}
return nil
}

View File

@ -1,6 +1,8 @@
package conf
import (
"strings"
"github.com/golang/protobuf/proto"
"github.com/v2fly/v2ray-core/v4/app/browserforwarder"
@ -11,7 +13,11 @@ type BrowserForwarderConfig struct {
ListenPort int32 `json:"listenPort"`
}
func (b BrowserForwarderConfig) Build() (proto.Message, error) {
func (b *BrowserForwarderConfig) Build() (proto.Message, error) {
b.ListenAddr = strings.TrimSpace(b.ListenAddr)
if b.ListenAddr != "" && b.ListenPort == 0 {
b.ListenPort = 54321
}
return &browserforwarder.Config{
ListenAddr: b.ListenAddr,
ListenPort: b.ListenPort,