package control import ( "context" "encoding/json" "net" "time" "github.com/pkg/errors" "github.com/xtaci/smux" "gitlab.com/wpetit/goweb/logger" ) type Control struct { encoder *json.Encoder decoder *json.Decoder stream *smux.Stream sess *smux.Session } func (c *Control) AuthRequest(credentials interface{}) (bool, error) { req := NewMessage(TypeAuthRequest, &AuthRequestPayload{ Credentials: credentials, }) res := NewMessage(TypeAuthResponse, nil) if err := c.reqRes(req, res); err != nil { return false, errors.WithStack(err) } authResPayload, ok := res.Payload.(*AuthResponsePayload) if !ok { return false, errors.WithStack(ErrUnexpectedMessage) } return authResPayload.Success, nil } type CloseStream func() func (c *Control) Proxy(ctx context.Context, network, address string) (net.Conn, error) { var ( stream *smux.Stream err error ) req := NewMessage(TypeProxyRequest, &ProxyRequestPayload{ Network: network, Address: address, }) ctx = logger.With(ctx, logger.F("network", network), logger.F("address", address)) logger.Debug(ctx, "proxying") if err := c.Write(req); err != nil { return nil, errors.WithStack(err) } logger.Debug(ctx, "opening stream") stream, err = c.sess.AcceptStream() if err != nil { return nil, errors.WithStack(err) } return stream, nil } func (c *Control) Listen(ctx context.Context, handlers Handlers) error { for { logger.Debug(ctx, "reading next message") req, err := c.Read() if err != nil { return errors.WithStack(err) } go func() { subCtx := logger.With(ctx, logger.F("messageType", req.Type)) handler, exists := handlers[req.Type] if !exists { logger.Error(subCtx, "no message handler registered") return } res, err := handler(subCtx, req) if err != nil { logger.Error(subCtx, "error while handling message", logger.E(err)) return } if res == nil { return } if err := c.Write(res); err != nil { logger.Error(subCtx, "error while write message response", logger.E(err)) return } }() } } func (c *Control) Read() (*Message, error) { message := &Message{} if err := c.read(message); err != nil { return nil, errors.WithStack(err) } return message, nil } func (c *Control) Write(m *Message) error { if err := c.write(m); err != nil { return errors.WithStack(err) } return nil } func (c *Control) reqRes(req *Message, res *Message) error { if err := c.write(req); err != nil { return errors.WithStack(err) } if err := c.read(res); err != nil { return errors.WithStack(err) } return nil } func (c *Control) read(m *Message) error { if err := c.decoder.Decode(m); err != nil { return errors.WithStack(err) } return nil } func (c *Control) write(m *Message) error { if err := c.stream.SetWriteDeadline(time.Now().Add(time.Second)); err != nil { return errors.WithStack(err) } if err := c.encoder.Encode(m); err != nil { return errors.WithStack(err) } return nil } func New(sess *smux.Session, controlStream *smux.Stream) *Control { return &Control{ encoder: json.NewEncoder(controlStream), decoder: json.NewDecoder(controlStream), sess: sess, stream: controlStream, } }