Skip to content

Instantly share code, notes, and snippets.

@mkmark
Forked from kallydev/main.go
Created August 30, 2022 14:43
Show Gist options
  • Save mkmark/015153c08acab7996a137bf27930ba56 to your computer and use it in GitHub Desktop.
Save mkmark/015153c08acab7996a137bf27930ba56 to your computer and use it in GitHub Desktop.
v2ray use gRPC API to add inbound example.
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"log"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/command"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/infra/conf"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/transport/internet"
)
func main() {
conn, err := grpc.Dial(fmt.Sprintf("%s:%d", "127.0.0.1", 50051), grpc.WithInsecure())
if err != nil {
log.Panicln(err)
}
client := command.NewHandlerServiceClient(conn)
_, err = client.AddInbound(context.Background(), &command.AddInboundRequest{
Inbound: &core.InboundHandlerConfig{
Tag: "inbound_vmess",
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: &net.PortRange{
From: 443,
To: 443,
},
Listen: &net.IPOrDomain{
Address: &net.IPOrDomain_Ip{
Ip: []byte{0, 0, 0, 0},
},
},
AllocationStrategy: &proxyman.AllocationStrategy{
Type: proxyman.AllocationStrategy_Always,
Concurrency: &proxyman.AllocationStrategy_AllocationStrategyConcurrency{
Value: 1,
},
// Refresh: &proxyman.AllocationStrategy_AllocationStrategyRefresh{
// Value: 2,
// },
},
StreamSettings: &internet.StreamConfig{
ProtocolName: "TCP",
TransportSettings: []*internet.TransportConfig{},
SecurityType: "TLS",
SecuritySettings: []*serial.TypedMessage{
serial.ToTypedMessage(&conf.TLSConfig{
Insecure: false,
InsecureCiphers: false,
Certs: []*conf.TLSCertConfig{
{
CertFile: "/etc/ssl/certs/example.crt",
// CertStr: nil,
KeyFile: "/etc/ssl/private/example.key",
// KeyStr: nil,
Usage: "encipherment",
},
},
ServerName: "example.com",
ALPN: &conf.StringList{
"h2",
"http/1.1",
},
DiableSystemRoot: false,
}),
},
SocketSettings: &internet.SocketConfig{
Mark: 0,
Tfo: internet.SocketConfig_Disable,
Tproxy: internet.SocketConfig_Off,
ReceiveOriginalDestAddress: false,
BindAddress: []byte{0, 0, 0, 0},
BindPort: 443,
},
},
ReceiveOriginalDestination: false,
SniffingSettings: &proxyman.SniffingConfig{
Enabled: true,
DestinationOverride: []string{
"http",
"tls",
},
},
}),
ProxySettings: serial.ToTypedMessage(&inbound.Config{
User: []*protocol.User{
{
Level: 0,
Email: "example@email.com",
Account: serial.ToTypedMessage(&vmess.Account{
Id: "5e79bb8f-a1f9-487f-a8ef-24e27bf5cfc7",
AlterId: 0,
SecuritySettings: &protocol.SecurityConfig{
Type: protocol.SecurityType_AUTO,
},
}),
},
},
Default: &inbound.DefaultConfig{
AlterId: 0,
Level: 0,
},
Detour: &inbound.DetourConfig{
To: "detour_tag",
},
SecureEncryptionOnly: true,
}),
},
})
if err != nil {
log.Panicln(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment