func GenerateCertificateForTest() *v2tls.Certificate { priv, err := rsa.GenerateKey(rand.Reader, 2048) common.Must(err) serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) if err != nil { log.Fatalf("failed to generate serial number: %s", err) } template := x509.Certificate{ SerialNumber: serialNumber, Subject: pkix.Name{ Organization: []string{"V2Ray Inc"}, }, NotBefore: time.Now(), NotAfter: time.Now().Add(time.Hour), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, DNSNames: []string{"www.v2ray.com"}, } derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) common.Must(err) certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}) return &v2tls.Certificate{ Certificate: certPEM, Key: keyPEM, } }
func pickPort() v2net.Port { listener, err := net.Listen("tcp4", ":0") common.Must(err) defer listener.Close() addr := listener.Addr().(*net.TCPAddr) return v2net.Port(addr.Port) }
func init() { common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_UDP, func(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { src := internet.DialerSourceFromContext(ctx) conn, err := internet.DialSystem(src, dest) if err != nil { return nil, err } // TODO: handle dialer options return internal.NewConnection(internal.NewConnectionID(src, dest), conn, internal.NoOpConnectionRecyler{}, internal.ReuseConnection(false)), nil })) }
func (v *AuthenticationReader) EnsureChunk() error { for { err := v.NextChunk() if err == nil { return nil } if err == errInsufficientBuffer { if v.buffer.IsEmpty() { v.buffer.Clear() } else { leftover := v.buffer.Bytes() common.Must(v.buffer.Reset(func(b []byte) (int, error) { return copy(b, leftover), nil })) } err = v.buffer.AppendSupplier(buf.ReadFrom(v.reader)) if err == nil { continue } } return err } }
func init() { common.Must(common.RegisterConfig((*dispatcher.Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { return NewDefaultDispatcher(ctx, config.(*dispatcher.Config)) })) }
func init() { common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_TCP, func() interface{} { return new(Config) })) }
func init() { common.Must(internet.RegisterTransportListener(internet.TransportProtocol_MKCP, ListenKCP)) }
func init() { common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { return NewServer(ctx, config.(*ServerConfig)) })) }
func init() { common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_TCP, Dial)) }
func init() { common.Must(internet.RegisterTransportListener(internet.TransportProtocol_WebSocket, ListenWS)) }
func init() { common.Must(common.RegisterConfig((*VideoConfig)(nil), NewVideoChat)) }
func init() { common.Must(common.RegisterConfig((*proxyman.OutboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { return New(ctx, config.(*proxyman.OutboundConfig)) })) }
func init() { common.Must(common.RegisterConfig((*Config)(nil), NewNoOpHeader)) common.Must(common.RegisterConfig((*ConnectionConfig)(nil), NewNoOpConnectionHeader)) }
func init() { common.Must(common.RegisterConfig((*Config)(nil), NewSRTP)) }
func init() { common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { return NewHttpAuthenticator(ctx, config.(*Config)) })) }