goesl

package module
v0.0.0-...-8c3e2db Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 17 Imported by: 0

README

License Test Status Go 1.20 Ready

Forked from 0x19's library because there are things I need to add quickly and might not want to be merged.

Check it out if you need something other than what is here.

FreeSWITCH Event Socket Library Wrapper for Go

GoESL is a small wrapper around FreeSWITCH Event Socket Library written in Go.

The point of this library is to fully implement FreeSWITCH ESL and bring outbound server and inbound client to the Go community 💚 📞

TODO

You can find what still needs to be done at GoESL TODO

Examples

There are few different types of examples that can be found at GoESL Examples.

Feel free to suggest more examples :)

Contributions / Issues?

For bugs please submit new issue. For questions, please ask in the discussion forum

Documentation

Index

Constants

View Source
const (
	LogLColorReset = "\033[0m"

	LogLColorRed    = "\033[31m"
	LogLColorGreen  = "\033[32m"
	LogLColorYellow = "\033[33m"
	LogLColorBlue   = "\033[34m"
	LogLColorPurple = "\033[35m"
	LogLColorCyan   = "\033[36m"
	LogLColorWhite  = "\033[37m"

	LogLColorBold      = "\033[1m"
	LogLColorBoldReset = "\033[22m"
	LogLColorUnderline = "\033[4m"
	LogLColorReversed  = "\033[7m"
)

Variables

View Source
var (
	EInvalidCommandProvided  = "Invalid command provided. Command cannot contain \\r and/or \\n. Provided command is: %s"
	ECouldNotReadMIMEHeaders = "Error while reading MIME headers: %s"
	EInvalidContentLength    = "Unable to get size of content-length: %s"
	EUnsuccessfulReply       = "Got error while reading from reply command: %s"
	ECouldNotReadyBody       = "Got error while reading reader body: %s"
	EUnsupportedMessageType  = "Unsupported message type! We got '%s'. Supported types are: %v "
	ECouldNotDecode          = "Could not decode/unescape message: %s"
	ECouldNotStartListener   = "Got error while attempting to start listener: %s"
	EListenerConnection      = "Listener connection error: %s"
	EInvalidServerAddr       = "Please make sure to pass along valid address. You've passed: \"%s\""
	EUnexpectedAuthHeader    = "Expected auth/request content type. Got %s"
	EInvalidPassword         = "Could not authenticate against freeswitch with provided password: %s"
	ECouldNotCreateMessage   = "Error while creating new message: %s"
	ECouldNotSendEvent       = "Must send at least one event header, detected `%d` header"
)
View Source
var (

	// Size of buffer when we read from connection.
	// 1024 << 6 == 65536
	ReadBufferSize = 1024 << 6

	// Freeswitch events that we can handle (have logic for it)
	AvailableMessageTypes = []string{"auth/request", "text/disconnect-notice", "text/event-json", "text/event-plain", "api/response", "command/reply"}
)

Functions

func Debug

func Debug(format string, v ...interface{})

Debug sends a debug log message.

func EnableDateTime

func EnableDateTime()

EnableDateTime enables date time in log messages.

func EnableDebug

func EnableDebug()

EnableDebug increases logging, more verbose (debug)

func EnableFatal

func EnableFatal()

EnablFatal so we only see crashes, mostly just for testing

func Error

func Error(format string, v ...interface{})

Error sends an error log message.

func Fatal

func Fatal(format string, v ...interface{})

Fatal sends a fatal log message and stop the execution of the program.

func Info

func Info(format string, v ...interface{})

Info sends an info log message.

func LogColorReset

func LogColorReset(level LogLevel) string

func LogColorSet

func LogColorSet(level LogLevel) string

func LogPrefix

func LogPrefix(level LogLevel) string

func LogTimestamp

func LogTimestamp() string

func SetOutputToFile

func SetOutputToFile(logFilePath *string)

func StringInSlice

func StringInSlice(str string, list []string) bool

StringInSlice - Will check if string in list. This is equivalent to python if x in []

func Warn

func Warn(format string, v ...interface{})

Warn sends an info log message.

Types

type Client

type Client struct {
	SocketConnection

	Proto   string `json:"freeswitch_protocol"`
	Addr    string `json:"freeswitch_addr"`
	Passwd  string `json:"freeswitch_password"`
	Timeout int    `json:"freeswitch_connection_timeout"`
}

Client - In case you need to do inbound dialing against freeswitch server in order to originate call or see sofia statuses or whatever else you came up with

func NewClient

func NewClient(host string, port uint, passwd string, timeout int) (*Client, error)

NewClient - Will initiate new client that will establish connection and attempt to authenticate against connected freeswitch server

func (*Client) Authenticate

func (c *Client) Authenticate() error

Authenticate - Method used to authenticate client against freeswitch. In case of any errors durring so we will return error.

func (*Client) EstablishConnection

func (c *Client) EstablishConnection() error

EstablishConnection - Will attempt to establish connection against freeswitch and create new SocketConnection

type LogLevel

type LogLevel uint32

LogLevel type.

const (
	// FatalLevel should be used in fatal situations, the app will exit.
	FatalLevel LogLevel = iota

	// ErrorLevel should be used when someone should really look at the error.
	ErrorLevel

	// InfoLevel should be used during normal operations.
	InfoLevel

	// WarnLevel should be used for things should be addressed at some point.
	WarnLevel

	// DebugLevel should be used only during development.
	DebugLevel
)

func (LogLevel) String

func (level LogLevel) String() string

type Message

type Message struct {
	Headers map[string]string
	Body    []byte
	// contains filtered or unexported fields
}

Message - Freeswitch Message that is received by GoESL. Message struct is here to help with parsing message and dumping its contents. In addition to that it's here to make sure received message is in fact message we wish/can support

func NewMessage

func NewMessage(r *bufio.Reader, autoParse bool) (*Message, error)

NewMessage - Will build and execute parsing against received freeswitch message. As return will give brand new Message{} for you to use it.

func (*Message) Dump

func (m *Message) Dump() (resp string)

Dump - Will return message prepared to be dumped out. It's like prettify message for output

func (*Message) GetCallUUID

func (m *Message) GetCallUUID() string

GetCallUUID - Will return Caller-Unique-ID

func (*Message) GetHeader

func (m *Message) GetHeader(key string) string

GetHeader - Will return message header value, or "" if the key is not set.

func (*Message) Parse

func (m *Message) Parse() error

Parse - Will parse out message received from Freeswitch and basically build it accordingly for later use. However, in case of any issues func will return error.

func (*Message) String

func (m *Message) String() string

String - Will return message representation as string

type OutboundServer

type OutboundServer struct {
	net.Listener

	Addr  string `json:"address"`
	Proto string

	Conns chan SocketConnection
}

OutboundServer - In case you need to start server, this Struct have it covered

func NewOutboundServer

func NewOutboundServer(addr string) (*OutboundServer, error)

NewOutboundServer - Will instanciate new outbound server

func (*OutboundServer) Start

func (s *OutboundServer) Start() error

Start - Will start new outbound server

func (*OutboundServer) Stop

func (s *OutboundServer) Stop()

Stop - Will close server connection once SIGTERM/Interrupt is received

type SocketConnection

type SocketConnection struct {
	net.Conn
	// contains filtered or unexported fields
}

Main connection against ESL - Gotta add more description here

func (*SocketConnection) Api

func (sc *SocketConnection) Api(command string) error

BgApi - Helper designed to attach api in front of the command so that you do not need to write it

func (*SocketConnection) BgApi

func (sc *SocketConnection) BgApi(command string) error

BgApi - Helper designed to attach bgapi in front of the command so that you do not need to write it

func (*SocketConnection) Close

func (c *SocketConnection) Close() error

Close - Will close down net connection and return error if error happen

func (*SocketConnection) Connect

func (sc *SocketConnection) Connect() error

Connect - Helper designed to help you handle connection. Each outbound server when handling needs to connect e.g. accept connection in order for you to do answer, hangup or do whatever else you wish to do

func (*SocketConnection) Connected

func (c *SocketConnection) Connected() (ok bool)

Connected checks if socket connected. Can be extended with pings TODO - add better connection handling

  • use heartbeat events from freeswitch
  • add bool to SocketConnection 'connected' that is changed on certain errors

func (*SocketConnection) Dial

func (c *SocketConnection) Dial(network string, addr string, timeout time.Duration) (net.Conn, error)

Dial - Will establish timedout dial against specified address. In this case, it will be freeswitch server

func (*SocketConnection) Execute

func (c *SocketConnection) Execute(command, args string, sync bool) (m *Message, err error)

Execute - Helper fuck to execute commands with its args and sync/async mode

func (*SocketConnection) ExecuteAnswer

func (sc *SocketConnection) ExecuteAnswer(args string, sync bool) (m *Message, err error)

ExecuteHangup - Helper desgned to help with executing Answer against active ESL session

func (*SocketConnection) ExecuteHangup

func (sc *SocketConnection) ExecuteHangup(uuid string, args string, sync bool) (m *Message, err error)

ExecuteHangup - Helper desgned to help with executing Hangup against active ESL session

func (*SocketConnection) ExecuteSet

func (sc *SocketConnection) ExecuteSet(key string, value string, sync bool) (m *Message, err error)

Set - Helper that you can use to execute SET application against active ESL session

func (*SocketConnection) ExecuteUUID

func (c *SocketConnection) ExecuteUUID(uuid string, command string, args string, sync bool) (m *Message, err error)

ExecuteUUID - Helper fuck to execute uuid specific commands with its args and sync/async mode

func (*SocketConnection) Exit

func (sc *SocketConnection) Exit() error

Exit - Used to send exit signal to ESL. It will basically hangup call and close connection

func (*SocketConnection) Handle

func (c *SocketConnection) Handle()

Handle - Will handle new messages and close connection when there are no messages left to process

func (*SocketConnection) OriginatorAddr

func (c *SocketConnection) OriginatorAddr() net.Addr

OriginatorAdd - Will return originator address known as net.RemoteAddr() This will actually be a freeswitch address

func (*SocketConnection) ReadMsg

func (c *SocketConnection) ReadMsg() (*Message, error)

ReadMsg - Will read message from channels and return them back accordingy. If error is received, error will be returned. If not, message will be returned back!

func (*SocketConnection) ReconnectIfNeeded

func (c *SocketConnection) ReconnectIfNeeded() (err error)

ReconnectIfNeeded if not connected, attempt reconnect if allowed

func (*SocketConnection) Send

func (c *SocketConnection) Send(cmd string) error

Send - Will send raw message to open net connection

func (*SocketConnection) SendEvent

func (c *SocketConnection) SendEvent(eventName string, eventHeaders []string, eventBody string) error

SendEvent - Will loop against passed event headers If you don't need a event body, pass in empty string ""

func (*SocketConnection) SendMany

func (c *SocketConnection) SendMany(cmds []string) error

SendMany - Will loop against passed commands and return 1st error if error happens

func (*SocketConnection) SendMsg

func (c *SocketConnection) SendMsg(msg map[string]string, uuid, data string) (m *Message, err error)

SendMsg - Basically this func will send message to the opened connection

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL