2017-10-16 11:44:03 +00:00
|
|
|
package pogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cloudflare/cloudflare-warp/tunnelrpc"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"zombiezen.com/go/capnproto2"
|
|
|
|
"zombiezen.com/go/capnproto2/pogs"
|
|
|
|
"zombiezen.com/go/capnproto2/rpc"
|
|
|
|
"zombiezen.com/go/capnproto2/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Authentication struct {
|
|
|
|
Key string
|
|
|
|
Email string
|
|
|
|
OriginCAKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalAuthentication(s tunnelrpc.Authentication, p *Authentication) error {
|
|
|
|
return pogs.Insert(tunnelrpc.Authentication_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalAuthentication(s tunnelrpc.Authentication) (*Authentication, error) {
|
|
|
|
p := new(Authentication)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.Authentication_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type TunnelRegistration struct {
|
|
|
|
Err string
|
2017-11-07 15:17:19 +00:00
|
|
|
Url string
|
2017-10-16 11:44:03 +00:00
|
|
|
LogLines []string
|
|
|
|
PermanentFailure bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalTunnelRegistration(s tunnelrpc.TunnelRegistration, p *TunnelRegistration) error {
|
|
|
|
return pogs.Insert(tunnelrpc.TunnelRegistration_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalTunnelRegistration(s tunnelrpc.TunnelRegistration) (*TunnelRegistration, error) {
|
|
|
|
p := new(TunnelRegistration)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.TunnelRegistration_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegistrationOptions struct {
|
2017-11-28 13:41:29 +00:00
|
|
|
ClientID string `capnp:"clientId"`
|
|
|
|
Version string
|
|
|
|
OS string `capnp:"os"`
|
|
|
|
ExistingTunnelPolicy tunnelrpc.ExistingTunnelPolicy
|
2018-02-20 21:13:56 +00:00
|
|
|
PoolName string `capnp:"poolName"`
|
2017-11-28 13:41:29 +00:00
|
|
|
Tags []Tag
|
2018-02-20 21:13:56 +00:00
|
|
|
ConnectionID uint8 `capnp:"connectionId"`
|
2017-11-28 13:41:29 +00:00
|
|
|
OriginLocalIP string `capnp:"originLocalIp"`
|
2018-02-20 21:13:56 +00:00
|
|
|
IsAutoupdated bool `capnp:"isAutoupdated"`
|
2017-10-16 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalRegistrationOptions(s tunnelrpc.RegistrationOptions, p *RegistrationOptions) error {
|
|
|
|
return pogs.Insert(tunnelrpc.RegistrationOptions_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalRegistrationOptions(s tunnelrpc.RegistrationOptions) (*RegistrationOptions, error) {
|
|
|
|
p := new(RegistrationOptions)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.RegistrationOptions_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tag struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerInfo struct {
|
|
|
|
LocationName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func MarshalServerInfo(s tunnelrpc.ServerInfo, p *ServerInfo) error {
|
|
|
|
return pogs.Insert(tunnelrpc.ServerInfo_TypeID, s.Struct, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnmarshalServerInfo(s tunnelrpc.ServerInfo) (*ServerInfo, error) {
|
|
|
|
p := new(ServerInfo)
|
|
|
|
err := pogs.Extract(p, tunnelrpc.ServerInfo_TypeID, s.Struct)
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type TunnelServer interface {
|
2017-11-07 15:17:19 +00:00
|
|
|
RegisterTunnel(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) (*TunnelRegistration, error)
|
2017-10-16 11:44:03 +00:00
|
|
|
GetServerInfo(ctx context.Context) (*ServerInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TunnelServer_ServerToClient(s TunnelServer) tunnelrpc.TunnelServer {
|
|
|
|
return tunnelrpc.TunnelServer_ServerToClient(TunnelServer_PogsImpl{s})
|
|
|
|
}
|
|
|
|
|
|
|
|
type TunnelServer_PogsImpl struct {
|
|
|
|
impl TunnelServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i TunnelServer_PogsImpl) RegisterTunnel(p tunnelrpc.TunnelServer_registerTunnel) error {
|
2017-11-07 15:17:19 +00:00
|
|
|
originCert, err := p.Params.OriginCert()
|
2017-10-16 11:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hostname, err := p.Params.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options, err := p.Params.Options()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pogsOptions, err := UnmarshalRegistrationOptions(options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
server.Ack(p.Options)
|
2017-11-07 15:17:19 +00:00
|
|
|
registration, err := i.impl.RegisterTunnel(p.Ctx, originCert, hostname, pogsOptions)
|
2017-10-16 11:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result, err := p.Results.NewResult()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return MarshalTunnelRegistration(result, registration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i TunnelServer_PogsImpl) GetServerInfo(p tunnelrpc.TunnelServer_getServerInfo) error {
|
|
|
|
server.Ack(p.Options)
|
|
|
|
serverInfo, err := i.impl.GetServerInfo(p.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result, err := p.Results.NewResult()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return MarshalServerInfo(result, serverInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TunnelServer_PogsClient struct {
|
|
|
|
Client capnp.Client
|
|
|
|
Conn *rpc.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c TunnelServer_PogsClient) Close() error {
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2017-11-07 15:17:19 +00:00
|
|
|
func (c TunnelServer_PogsClient) RegisterTunnel(ctx context.Context, originCert []byte, hostname string, options *RegistrationOptions) (*TunnelRegistration, error) {
|
2017-10-16 11:44:03 +00:00
|
|
|
client := tunnelrpc.TunnelServer{Client: c.Client}
|
|
|
|
promise := client.RegisterTunnel(ctx, func(p tunnelrpc.TunnelServer_registerTunnel_Params) error {
|
2017-11-07 15:17:19 +00:00
|
|
|
err := p.SetOriginCert(originCert)
|
2017-10-16 11:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.SetHostname(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registrationOptions, err := p.NewOptions()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = MarshalRegistrationOptions(registrationOptions, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
retval, err := promise.Result().Struct()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return UnmarshalTunnelRegistration(retval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c TunnelServer_PogsClient) GetServerInfo(ctx context.Context) (*ServerInfo, error) {
|
|
|
|
client := tunnelrpc.TunnelServer{Client: c.Client}
|
|
|
|
promise := client.GetServerInfo(ctx, func(p tunnelrpc.TunnelServer_getServerInfo_Params) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
retval, err := promise.Result().Struct()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return UnmarshalServerInfo(retval)
|
|
|
|
}
|