mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-14 05:16:25 -05:00
cleanup http proxy package
This commit is contained in:
parent
b47c1ca609
commit
9b07ffd68f
@ -1,22 +1,21 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import "crypto/tls"
|
||||||
"crypto/tls"
|
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
// CertificateConfig is the config for TLS certificates used in HTTP proxy.
|
||||||
type CertificateConfig struct {
|
type CertificateConfig struct {
|
||||||
Domain string
|
Domain string
|
||||||
Certificate tls.Certificate
|
Certificate tls.Certificate
|
||||||
}
|
}
|
||||||
|
|
||||||
type TlsConfig struct {
|
// TlsConfig is the config for TLS connections.
|
||||||
|
type TLSConfig struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Certs []*CertificateConfig
|
Certs []*CertificateConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TlsConfig) GetConfig() *tls.Config {
|
// GetConfig returns corresponding tls.Config.
|
||||||
|
func (this *TLSConfig) GetConfig() *tls.Config {
|
||||||
if !this.Enabled {
|
if !this.Enabled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -35,19 +34,11 @@ func (this *TlsConfig) GetConfig() *tls.Config {
|
|||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config for HTTP proxy server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
OwnHosts []v2net.Address
|
TLSConfig *TLSConfig
|
||||||
TlsConfig *TlsConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *Config) IsOwnHost(host v2net.Address) bool {
|
|
||||||
for _, ownHost := range this.OwnHosts {
|
|
||||||
if ownHost.Equals(host) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClientConfig for HTTP proxy client.
|
||||||
type ClientConfig struct {
|
type ClientConfig struct {
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler
|
||||||
func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
|
func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
Domain string `json:"domain"`
|
Domain string `json:"domain"`
|
||||||
@ -30,7 +30,8 @@ func (this *CertificateConfig) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TlsConfig) UnmarshalJSON(data []byte) error {
|
// UnmarshalJSON implements json.Unmarshaler
|
||||||
|
func (this *TLSConfig) UnmarshalJSON(data []byte) error {
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
Enabled bool `json:"enable"`
|
Enabled bool `json:"enable"`
|
||||||
Certs []*CertificateConfig `json:"certs"`
|
Certs []*CertificateConfig `json:"certs"`
|
||||||
@ -45,26 +46,17 @@ func (this *TlsConfig) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler
|
||||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
Hosts []v2net.AddressJson `json:"ownHosts"`
|
Tls *TLSConfig `json:"tls"`
|
||||||
Tls *TlsConfig `json:"tls"`
|
|
||||||
}
|
}
|
||||||
jsonConfig := new(JsonConfig)
|
jsonConfig := new(JsonConfig)
|
||||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
this.OwnHosts = make([]v2net.Address, len(jsonConfig.Hosts), len(jsonConfig.Hosts)+1)
|
|
||||||
for idx, host := range jsonConfig.Hosts {
|
|
||||||
this.OwnHosts[idx] = host.Address
|
|
||||||
}
|
|
||||||
|
|
||||||
v2rayHost := v2net.DomainAddress("local.v2ray.com")
|
this.TLSConfig = jsonConfig.Tls
|
||||||
if !this.IsOwnHost(v2rayHost) {
|
|
||||||
this.OwnHosts = append(this.OwnHosts, v2rayHost)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.TlsConfig = jsonConfig.Tls
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,3 @@
|
|||||||
// +build json
|
// +build json
|
||||||
|
|
||||||
package http_test
|
package http_test
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
. "github.com/v2ray/v2ray-core/proxy/http"
|
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestOwnHosts(t *testing.T) {
|
|
||||||
assert := assert.On(t)
|
|
||||||
|
|
||||||
rawJson := `{
|
|
||||||
"ownHosts": [
|
|
||||||
"127.0.0.1",
|
|
||||||
"google.com"
|
|
||||||
]
|
|
||||||
}`
|
|
||||||
|
|
||||||
config := new(Config)
|
|
||||||
err := json.Unmarshal([]byte(rawJson), config)
|
|
||||||
assert.Error(err).IsNil()
|
|
||||||
assert.Bool(config.IsOwnHost(v2net.IPAddress([]byte{127, 0, 0, 1}))).IsTrue()
|
|
||||||
assert.Bool(config.IsOwnHost(v2net.DomainAddress("google.com"))).IsTrue()
|
|
||||||
assert.Bool(config.IsOwnHost(v2net.DomainAddress("local.v2ray.com"))).IsTrue()
|
|
||||||
assert.Bool(config.IsOwnHost(v2net.DomainAddress("v2ray.com"))).IsFalse()
|
|
||||||
}
|
|
||||||
|
@ -22,7 +22,8 @@ import (
|
|||||||
"github.com/v2ray/v2ray-core/transport/ray"
|
"github.com/v2ray/v2ray-core/transport/ray"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HttpProxyServer struct {
|
// Server is a HTTP proxy server.
|
||||||
|
type Server struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
accepting bool
|
accepting bool
|
||||||
packetDispatcher dispatcher.PacketDispatcher
|
packetDispatcher dispatcher.PacketDispatcher
|
||||||
@ -32,18 +33,18 @@ type HttpProxyServer struct {
|
|||||||
listeningAddress v2net.Address
|
listeningAddress v2net.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHttpProxyServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *HttpProxyServer {
|
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Server {
|
||||||
return &HttpProxyServer{
|
return &Server{
|
||||||
packetDispatcher: packetDispatcher,
|
packetDispatcher: packetDispatcher,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) Port() v2net.Port {
|
func (this *Server) Port() v2net.Port {
|
||||||
return this.listeningPort
|
return this.listeningPort
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) Close() {
|
func (this *Server) Close() {
|
||||||
this.accepting = false
|
this.accepting = false
|
||||||
if this.tcpListener != nil {
|
if this.tcpListener != nil {
|
||||||
this.Lock()
|
this.Lock()
|
||||||
@ -53,7 +54,7 @@ func (this *HttpProxyServer) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) Listen(address v2net.Address, port v2net.Port) error {
|
func (this *Server) Listen(address v2net.Address, port v2net.Port) error {
|
||||||
if this.accepting {
|
if this.accepting {
|
||||||
if this.listeningPort == port && this.listeningAddress.Equals(address) {
|
if this.listeningPort == port && this.listeningAddress.Equals(address) {
|
||||||
return nil
|
return nil
|
||||||
@ -64,9 +65,9 @@ func (this *HttpProxyServer) Listen(address v2net.Address, port v2net.Port) erro
|
|||||||
this.listeningPort = port
|
this.listeningPort = port
|
||||||
this.listeningAddress = address
|
this.listeningAddress = address
|
||||||
|
|
||||||
var tlsConfig *tls.Config = nil
|
var tlsConfig *tls.Config
|
||||||
if this.config.TlsConfig != nil {
|
if this.config.TLSConfig != nil {
|
||||||
tlsConfig = this.config.TlsConfig.GetConfig()
|
tlsConfig = this.config.TLSConfig.GetConfig()
|
||||||
}
|
}
|
||||||
tcpListener, err := hub.ListenTCP(address, port, this.handleConnection, tlsConfig)
|
tcpListener, err := hub.ListenTCP(address, port, this.handleConnection, tlsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,7 +104,7 @@ func parseHost(rawHost string, defaultPort v2net.Port) (v2net.Destination, error
|
|||||||
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
|
return v2net.TCPDestination(v2net.DomainAddress(host), port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) handleConnection(conn *hub.Connection) {
|
func (this *Server) handleConnection(conn *hub.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
@ -133,7 +134,7 @@ func (this *HttpProxyServer) handleConnection(conn *hub.Connection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) {
|
func (this *Server) handleConnect(request *http.Request, destination v2net.Destination, reader io.Reader, writer io.Writer) {
|
||||||
response := &http.Response{
|
response := &http.Response{
|
||||||
Status: "200 OK",
|
Status: "200 OK",
|
||||||
StatusCode: 200,
|
StatusCode: 200,
|
||||||
@ -155,7 +156,7 @@ func (this *HttpProxyServer) handleConnect(request *http.Request, destination v2
|
|||||||
this.transport(reader, writer, ray)
|
this.transport(reader, writer, ray)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
|
func (this *Server) transport(input io.Reader, output io.Writer, ray ray.InboundRay) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
defer wg.Wait()
|
defer wg.Wait()
|
||||||
@ -204,7 +205,7 @@ func StripHopByHopHeaders(request *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) {
|
func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destination, reader *bufio.Reader, writer io.Writer) {
|
||||||
if len(request.URL.Host) <= 0 {
|
if len(request.URL.Host) <= 0 {
|
||||||
hdr := http.Header(make(map[string][]string))
|
hdr := http.Header(make(map[string][]string))
|
||||||
hdr.Set("Connection", "close")
|
hdr.Set("Connection", "close")
|
||||||
@ -273,7 +274,7 @@ func init() {
|
|||||||
if !space.HasApp(dispatcher.APP_ID) {
|
if !space.HasApp(dispatcher.APP_ID) {
|
||||||
return nil, internal.ErrorBadConfiguration
|
return nil, internal.ErrorBadConfiguration
|
||||||
}
|
}
|
||||||
return NewHttpProxyServer(
|
return NewServer(
|
||||||
rawConfig.(*Config),
|
rawConfig.(*Config),
|
||||||
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
|
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
|
||||||
})
|
})
|
||||||
|
@ -52,7 +52,7 @@ func TestNormalGetRequest(t *testing.T) {
|
|||||||
|
|
||||||
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
|
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
|
||||||
|
|
||||||
httpProxy := NewHttpProxyServer(&Config{}, testPacketDispatcher)
|
httpProxy := NewServer(&Config{}, testPacketDispatcher)
|
||||||
defer httpProxy.Close()
|
defer httpProxy.Close()
|
||||||
|
|
||||||
port := v2nettesting.PickPort()
|
port := v2nettesting.PickPort()
|
||||||
|
Loading…
Reference in New Issue
Block a user