Skip to content

Commit

Permalink
🔇 silent changes: updated comment and document for a lot of functions #…
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 12, 2024
1 parent f57af06 commit 44e33c6
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 214 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module github.com/pnguyen215/voipkit
go 1.20

require (
github.com/json-iterator/go v1.1.12
github.com/nyaruka/phonenumbers v1.1.6
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/text v0.3.7 // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nyaruka/phonenumbers v1.1.6 h1:DcueYq7QrOArAprAYNoQfDgp0KetO4LqtnBtQC6Wyes=
github.com/nyaruka/phonenumbers v1.1.6/go.mod h1:yShPJHDSH3aTKzCbXyVxNpbl2kA+F+Ne5Pun/MvFRos=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 h1:pVgRXcIictcr+lBQIFeiwuwtDIs4eL21OuM9nyAADmo=
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
Expand Down
54 changes: 27 additions & 27 deletions pkg/ami/ami_cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func NewAMICdr() *AMICdr {
r := &AMICdr{}
r.SetEvent(config.AmiListenerEventCdr)
r.SetExtenSplitterSymbol("-")
r.SetSymbol("-")
return r
}

Expand Down Expand Up @@ -203,17 +203,17 @@ func (r *AMICdr) SetDirection(value string) *AMICdr {
return r
}

func (r *AMICdr) SetFlowCall(value string) *AMICdr {
r.FlowCall = value
func (r *AMICdr) SetDesc(value string) *AMICdr {
r.Desc = value
return r
}

func (r *AMICdr) SetTypeDirection(value string) *AMICdr {
r.TypeDirection = TrimStringSpaces(value)
func (r *AMICdr) SetType(value string) *AMICdr {
r.Type = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetUserExten(value string) *AMICdr {
func (r *AMICdr) SetUserExtension(value string) *AMICdr {
r.UserExtension = TrimStringSpaces(value)
return r
}
Expand All @@ -223,8 +223,8 @@ func (r *AMICdr) SetPhoneNumber(value string) *AMICdr {
return r
}

func (r *AMICdr) SetExtenSplitterSymbol(value string) *AMICdr {
r.ExtenSplitterSymbol = TrimStringSpaces(value)
func (r *AMICdr) SetSymbol(value string) *AMICdr {
r.symbol = TrimStringSpaces(value)
return r
}

Expand Down Expand Up @@ -298,19 +298,19 @@ func (r *AMICdr) IsCdrOutbound() bool {
}

func (r *AMICdr) IsCdrInboundDial() bool {
return strings.EqualFold(r.TypeDirection, config.AmiTypeInboundDialDirection)
return strings.EqualFold(r.Type, config.AmiTypeInboundDialDirection)
}

func (r *AMICdr) IsCdrInboundQueue() bool {
return strings.EqualFold(r.TypeDirection, config.AmiTypeInboundQueueDirection)
return strings.EqualFold(r.Type, config.AmiTypeInboundQueueDirection)
}

func (r *AMICdr) IsCdrOutboundNormal() bool {
return strings.EqualFold(r.TypeDirection, config.AmiTypeOutboundNormalDirection)
return strings.EqualFold(r.Type, config.AmiTypeOutboundNormalDirection)
}

func (r *AMICdr) IsCdrOutboundChanSpy() bool {
return strings.EqualFold(r.TypeDirection, config.AmiLastApplicationChanSpy)
return strings.EqualFold(r.Type, config.AmiLastApplicationChanSpy)
}

func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
Expand Down Expand Up @@ -344,48 +344,48 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {

// detect outbound, inbound
// if the field destination is phone number, so mark this cdr belong to outbound, otherwise mark as inbound
form := "flow_call_from_'%v'_to_'%v'"
form := "CDR.call_from_'%v'_to_'%v'"
phone := RemoveStringPrefix(r.Destination, e.PhonePrefix...)
if IsPhoneNumberWith(phone, e.Region) {
if VerifyPhoneNo(phone, e.Region) {
flow := fmt.Sprintf(form, r.Channel, phone)
r.SetFlowCall(flow)
r.SetDesc(flow)
r.SetDirection(config.AmiOutboundDirection)
r.SetTypeDirection(config.AmiTypeOutboundNormalDirection)
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
r.SetType(config.AmiTypeOutboundNormalDirection)
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetPhoneNumber(phone)
} else {
var inCase bool = false
// from outbound chan-spy
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationChanSpy) {
inCase = true
flow := fmt.Sprintf(form, r.Channel, r.LastData)
r.SetFlowCall(flow)
r.SetTypeDirection(config.AmiTypeChanSpyDirection)
r.SetDesc(flow)
r.SetType(config.AmiTypeChanSpyDirection)
r.SetDirection(config.AmiOutboundDirection)
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
}
// from inbound dial
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationDial) {
inCase = true
flow := fmt.Sprintf(form, r.Source, r.DestinationChannel)
r.SetFlowCall(flow)
r.SetDesc(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetTypeDirection(config.AmiTypeInboundDialDirection)
r.SetUserExten(strings.Split(r.DestinationChannel, r.ExtenSplitterSymbol)[0])
r.SetType(config.AmiTypeInboundDialDirection)
r.SetUserExtension(strings.Split(r.DestinationChannel, r.symbol)[0])
r.SetPhoneNumber(r.Source)
}
// from inbound queue
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationQueue) {
inCase = true
flow := fmt.Sprintf(form, r.Source, r.Channel)
r.SetFlowCall(flow)
r.SetDesc(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetTypeDirection(config.AmiTypeInboundQueueDirection)
r.SetUserExten(strings.Split(r.Channel, r.ExtenSplitterSymbol)[0])
r.SetType(config.AmiTypeInboundQueueDirection)
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetPhoneNumber(r.Source)
}
if !inCase {
log.Printf("ParseCdr, CDR exception case = %v", JsonString(r))
log.Printf("ParseCdr, CDR got an error exception case:: %v", JsonString(r))
}
}
return r
Expand Down
34 changes: 17 additions & 17 deletions pkg/ami/ami_chanspy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ func (s *AMIPayloadChanspy) SetChannelProtocol(value string) *AMIPayloadChanspy
return s
}

func (s *AMIPayloadChanspy) SetAllowDebug(value bool) *AMIPayloadChanspy {
s.AllowDebug = value
func (s *AMIPayloadChanspy) SetDebugMode(value bool) *AMIPayloadChanspy {
s.DebugMode = value
return s
}

func (s *AMIPayloadChanspy) CommandChanspy(channelExten string) string {
func (s *AMIPayloadChanspy) CommandChanspy(c_extension string) string {
if IsStringEmpty(s.Join) {
return ""
}
if IsStringEmpty(channelExten) {
if IsStringEmpty(c_extension) {
return ""
}
if strings.EqualFold(s.Join, config.AmiChanspySpy) {
return channelExten
return c_extension
}
if strings.EqualFold(s.Join, config.AmiChanspyWhisper) {
return fmt.Sprintf("%s,w", channelExten)
return fmt.Sprintf("%s,w", c_extension)
}
if strings.EqualFold(s.Join, config.AmiChanspyBarge) {
return fmt.Sprintf("%s,B", channelExten)
return fmt.Sprintf("%s,B", c_extension)
}
return channelExten
return c_extension
}

// Chanspy
Expand All @@ -72,31 +72,31 @@ func Chanspy(ctx context.Context, s AMISocket, ch AMIPayloadChanspy) (AMIResultR
log.Panic(config.AmiErrorInvalidChanspy, "\n", msg)
}
if IsStringEmpty(ch.SourceExten) {
return AMIResultRawLevel{}, fmt.Errorf("Source exten is required")
return AMIResultRawLevel{}, fmt.Errorf("Source extension is required")
}
if IsStringEmpty(ch.CurrentExten) {
return AMIResultRawLevel{}, fmt.Errorf("Current exten is required")
return AMIResultRawLevel{}, fmt.Errorf("Current extension is required")
}
sourceValid, err := HasSIPPeerStatus(ctx, s, ch.SourceExten)
source_extension_verify, err := HasSIPPeerStatus(ctx, s, ch.SourceExten)
if err != nil {
return AMIResultRawLevel{}, err
}
if !sourceValid {
return AMIResultRawLevel{}, fmt.Errorf("Source exten '%v' not found", ch.SourceExten)
if !source_extension_verify {
return AMIResultRawLevel{}, fmt.Errorf("Source extension '%v' not found", ch.SourceExten)
}
currentValid, err := HasSIPPeerStatus(ctx, s, ch.CurrentExten)
current_extension_verify, err := HasSIPPeerStatus(ctx, s, ch.CurrentExten)
if err != nil {
return AMIResultRawLevel{}, err
}
if !currentValid {
return AMIResultRawLevel{}, fmt.Errorf("Current exten '%v' not found", ch.CurrentExten)
if !current_extension_verify {
return AMIResultRawLevel{}, fmt.Errorf("Current extension '%v' not found", ch.CurrentExten)
}
channel := NewChannel().SetChannelProtocol(ch.ChannelProtocol)
sourceExt := channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", ch.SourceExten))
currentExt := channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", ch.CurrentExten))
channelId := ch.CommandChanspy(sourceExt)
cmd := fmt.Sprintf("channel originate %s application ChanSpy %s", currentExt, channelId)
if ch.AllowDebug {
if ch.DebugMode {
log.Printf("Chanspy command: %v \n", cmd)
log.Printf("Chanspy channel_id: %v \n", channelId)
log.Printf("Chanspy source.ext: %v \n", sourceExt)
Expand Down
60 changes: 60 additions & 0 deletions pkg/ami/ami_dial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ami

import (
"bufio"
"context"
"log"
"net"
"net/textproto"

"github.com/pnguyen215/voipkit/pkg/ami/config"
)

// OpenContext
func OpenContext(conn net.Conn) (*AMI, context.Context) {
ctx, cancel := context.WithCancel(context.Background())
client := &AMI{
Reader: textproto.NewReader(bufio.NewReader(conn)),
Writer: bufio.NewWriter(conn),
Conn: conn,
Cancel: cancel,
}
// checking conn available
if conn != nil {
addr := conn.RemoteAddr().String()
_socket, err := NewAMISocketWith(ctx, addr)

if err == nil {
client.Socket = _socket
log.Printf("OpenContext, cloning (addr: %v) socket connection succeeded", addr)
}
}
return client, ctx
}

// OpenDial
func OpenDial(ip string, port int) (net.Conn, error) {
return OpenDialWith(config.AmiNetworkTcpKey, ip, port)
}

// OpenDialWith
func OpenDialWith(network, ip string, port int) (net.Conn, error) {
if !config.AmiNetworkKeys[network] {
return nil, AMIErrorNew("AMI: Invalid network")
}
if ip == "" {
return nil, AMIErrorNew("AMI: IP must be not empty")
}
if port <= 0 {
return nil, AMIErrorNew("AMI: Port must be positive number")
}
host, _port, _ := DecodeIp(ip)
if len(host) > 0 && len(_port) > 0 {
form := net.JoinHostPort(host, _port)
log.Printf("AMI: (IP decoded) dial connection = %v", form)
return net.Dial(network, form)
}
form := RemoveProtocol(ip, port)
log.Printf("AMI: dial connection = %v", form)
return net.Dial(network, form)
}
Loading

0 comments on commit 44e33c6

Please sign in to comment.