本文整理汇总了Golang中v2ray/com/core/common/net.Port函数的典型用法代码示例。如果您正苦于以下问题:Golang Port函数的具体用法?Golang Port怎么用?Golang Port使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Port函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Process
func (s *Server) Process(ctx context.Context, network v2net.Network, conn internet.Connection) error {
conn.SetReusable(false)
timedReader := v2net.NewTimeOutReader(s.config.Timeout, conn)
reader := bufio.OriginalReaderSize(timedReader, 2048)
request, err := http.ReadRequest(reader)
if err != nil {
if errors.Cause(err) != io.EOF {
log.Warning("HTTP: Failed to read http request: ", err)
}
return err
}
log.Info("HTTP: Request to Method [", request.Method, "] Host [", request.Host, "] with URL [", request.URL, "]")
defaultPort := v2net.Port(80)
if strings.ToLower(request.URL.Scheme) == "https" {
defaultPort = v2net.Port(443)
}
host := request.Host
if len(host) == 0 {
host = request.URL.Host
}
dest, err := parseHost(host, defaultPort)
if err != nil {
log.Warning("HTTP: Malformed proxy host (", host, "): ", err)
return err
}
log.Access(conn.RemoteAddr(), request.URL, log.AccessAccepted, "")
ctx = proxy.ContextWithDestination(ctx, dest)
if strings.ToUpper(request.Method) == "CONNECT" {
return s.handleConnect(ctx, request, reader, conn)
} else {
return s.handlePlainHTTP(ctx, request, reader, conn)
}
}
示例2: parseStringPort
func parseStringPort(data []byte) (v2net.Port, v2net.Port, error) {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
pair := strings.SplitN(s, "-", 2)
if len(pair) == 0 {
return v2net.Port(0), v2net.Port(0), v2net.ErrInvalidPortRange
}
if len(pair) == 1 {
port, err := v2net.PortFromString(pair[0])
return port, port, err
}
fromPort, err := v2net.PortFromString(pair[0])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
toPort, err := v2net.PortFromString(pair[1])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
return fromPort, toPort, nil
}
示例3: Start
func (server *Server) Start() (v2net.Destination, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(server.Port),
Zone: "",
})
if err != nil {
return v2net.Destination{}, err
}
server.Port = v2net.Port(listener.Addr().(*net.TCPAddr).Port)
server.listener = listener
go server.acceptConnections(listener)
localAddr := listener.Addr().(*net.TCPAddr)
return v2net.TCPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil
}
示例4: Start
func (server *Server) Start() (v2net.Destination, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(server.Port),
Zone: "",
})
if err != nil {
return nil, err
}
server.Port = v2net.Port(conn.LocalAddr().(*net.UDPAddr).Port)
server.conn = conn
go server.handleConnection(conn)
localAddr := conn.LocalAddr().(*net.UDPAddr)
return v2net.UDPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil
}
示例5: TestNormalGetRequest
func TestNormalGetRequest(t *testing.T) {
assert := assert.On(t)
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
port := v2net.Port(dice.Roll(20000) + 10000)
httpProxy := NewServer(
&Config{},
testPacketDispatcher,
&proxy.InboundHandlerMeta{
Address: v2net.LocalHostIP,
Port: port,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
}})
defer httpProxy.Close()
err := httpProxy.Start()
assert.Error(err).IsNil()
assert.Port(port).Equals(httpProxy.Port())
httpClient := &http.Client{}
resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(400)
}
示例6: TestHttpProxy
func TestHttpProxy(t *testing.T) {
assert := assert.On(t)
httpServer := &v2http.Server{
Port: v2net.Port(50042),
PathHandler: make(map[string]http.HandlerFunc),
}
_, err := httpServer.Start()
assert.Error(err).IsNil()
defer httpServer.Close()
assert.Error(InitializeServerSetOnce("test_5")).IsNil()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:50040/")
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("http://127.0.0.1:50042/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(200)
content, err := ioutil.ReadAll(resp.Body)
assert.Error(err).IsNil()
assert.String(string(content)).Equals("Home")
CloseAllServers()
}
示例7: TestHubSocksOption
func TestHubSocksOption(t *testing.T) {
assert := assert.On(t)
if os.Geteuid() != 0 {
// This test case requires root permission.
return
}
hub, err := ListenUDP(v2net.LocalHostIP, v2net.Port(0), ListenOption{
Callback: func(*buf.Buffer, *proxy.SessionInfo) {},
ReceiveOriginalDest: true,
})
assert.Error(err).IsNil()
conn := hub.Connection()
fd, err := internal.GetSysFd(conn)
assert.Error(err).IsNil()
val, err := syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
val, err = syscall.GetsockoptInt(fd, syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR)
assert.Error(err).IsNil()
assert.Int(val).Equals(1)
}
示例8: UnmarshalJSON
func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Port uint16 `json:"port"`
Listen *v2net.AddressJson `json:"listen"`
Protocol string `json:"protocol"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
AllowPassive bool `json:"allowPassive"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("Point: Failed to parse inbound config: " + err.Error())
}
this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP
if jsonConfig.Listen != nil {
if jsonConfig.Listen.Address.Family().IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
}
this.ListenOn = jsonConfig.Listen.Address
}
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
this.AllowPassiveConnection = jsonConfig.AllowPassive
return nil
}
示例9: TestIPResolution
func TestIPResolution(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
r := router.NewRouter(&router.Config{}, space)
space.BindApp(router.APP_ID, r)
dnsServer := dnsserver.NewCacheServer(space, &dns.Config{
Hosts: map[string]*v2net.IPOrDomain{
"v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP),
},
})
space.BindApp(dns.APP_ID, dnsServer)
freedom := NewFreedomConnection(
&Config{DomainStrategy: Config_USE_IP},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamConfig{
Network: v2net.Network_RawTCP,
},
})
space.Initialize()
ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
assert.Destination(ipDest).IsTCP()
assert.Address(ipDest.Address).Equals(v2net.LocalHostIP)
}
示例10: start
func (v *UDPHub) start() {
v.cancel.WaitThread()
defer v.cancel.FinishThread()
oobBytes := make([]byte, 256)
for v.Running() {
buffer := buf.NewSmall()
var noob int
var addr *net.UDPAddr
err := buffer.AppendSupplier(func(b []byte) (int, error) {
n, nb, _, a, e := ReadUDPMsg(v.conn, b, oobBytes)
noob = nb
addr = a
return n, e
})
if err != nil {
log.Info("UDP|Hub: Failed to read UDP msg: ", err)
buffer.Release()
continue
}
session := new(proxy.SessionInfo)
session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
if v.option.ReceiveOriginalDest && noob > 0 {
session.Destination = RetrieveOriginalDest(oobBytes[:noob])
}
v.queue.Enqueue(UDPPayload{
payload: buffer,
session: session,
})
}
}
示例11: start
func (this *UDPHub) start() {
this.cancel.WaitThread()
defer this.cancel.FinishThread()
oobBytes := make([]byte, 256)
for this.Running() {
buffer := this.pool.Allocate()
nBytes, noob, _, addr, err := ReadUDPMsg(this.conn, buffer.Value, oobBytes)
if err != nil {
log.Info("UDP|Hub: Failed to read UDP msg: ", err)
buffer.Release()
continue
}
buffer.Slice(0, nBytes)
session := new(proxy.SessionInfo)
session.Source = v2net.UDPDestination(v2net.IPAddress(addr.IP), v2net.Port(addr.Port))
if this.option.ReceiveOriginalDest && noob > 0 {
session.Destination = RetrieveOriginalDest(oobBytes[:noob])
}
this.queue.Enqueue(UDPPayload{
payload: buffer,
session: session,
})
}
}
示例12: TestResponseWrite
func TestResponseWrite(t *testing.T) {
assert := assert.On(t)
response := Socks5Response{
socksVersion,
ErrorSuccess,
AddrTypeIPv4,
[4]byte{0x72, 0x72, 0x72, 0x72},
"",
[16]byte{},
v2net.Port(53),
}
buffer := alloc.NewLocalBuffer(2048).Clear()
defer buffer.Release()
response.Write(buffer)
expectedBytes := []byte{
socksVersion,
ErrorSuccess,
byte(0x00),
AddrTypeIPv4,
0x72, 0x72, 0x72, 0x72,
byte(0x00), byte(0x035),
}
assert.Bytes(buffer.Value).Equals(expectedBytes)
}
示例13: TestDnsAdd
func TestDnsAdd(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
outboundHandlerManager.SetDefaultHandler(
freedom.NewFreedomConnection(
&freedom.Config{},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
},
}))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
domain := "local.v2ray.com"
server := NewCacheServer(space, &Config{
NameServers: []v2net.Destination{
v2net.UDPDestination(v2net.IPAddress([]byte{8, 8, 8, 8}), v2net.Port(53)),
},
})
space.BindApp(APP_ID, server)
space.Initialize()
ips := server.Get(domain)
assert.Int(len(ips)).Equals(1)
assert.IP(ips[0].To4()).Equals(net.IP([]byte{127, 0, 0, 1}))
}
示例14: TestBlockHTTP
func TestBlockHTTP(t *testing.T) {
assert := assert.On(t)
httpServer := &v2http.Server{
Port: net.Port(50042),
PathHandler: make(map[string]http.HandlerFunc),
}
_, err := httpServer.Start()
assert.Error(err).IsNil()
defer httpServer.Close()
assert.Error(InitializeServerSetOnce("test_5")).IsNil()
transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:50040/")
},
}
client := &http.Client{
Transport: transport,
}
resp, err := client.Get("http://127.0.0.1:50049/")
assert.Error(err).IsNil()
assert.Int(resp.StatusCode).Equals(403)
CloseAllServers()
}
示例15: TestIPResolution
func TestIPResolution(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space)
space.BindApp(router.APP_ID, r)
dnsServer := dns.NewCacheServer(space, &dns.Config{
Hosts: map[string]net.IP{
"v2ray.com": net.IP([]byte{127, 0, 0, 1}),
},
})
space.BindApp(dns.APP_ID, dnsServer)
freedom := NewFreedomConnection(
&Config{DomainStrategy: DomainStrategyUseIP},
space,
&proxy.OutboundHandlerMeta{
Address: v2net.AnyIP,
StreamSettings: &internet.StreamSettings{
Type: internet.StreamConnectionTypeRawTCP,
},
})
space.Initialize()
ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
assert.Destination(ipDest).IsTCP()
assert.Address(ipDest.Address()).Equals(v2net.LocalHostIP)
}