diff --git a/config.json b/config.json new file mode 100644 index 0000000..324177e --- /dev/null +++ b/config.json @@ -0,0 +1,27 @@ +{ + "name": "SIP Trunk IP Auth", + "call_settings": { + "limit": 5, + "timeout": 3600 + }, + "auth": { + "type": "ip" + }, + "endpoints": [ + { + "host": "50.192.97.226", + "port": 5060, + "signalling": "proxy", + "media": "proxy", + "description": "SIP Trunk Endpoint", + "rweight": 1 + } + ], + "strip": 0, + "prefix": "", + "notifications": { + "overmaxcalllimit": "email@example.com", + "endpointfailure": "email@example.com" + } + } + \ No newline at end of file diff --git a/dsipclient/data_endpointgrouprequest.go b/dsipclient/data_endpointgrouprequest.go index 017ee36..c51f37c 100644 --- a/dsipclient/data_endpointgrouprequest.go +++ b/dsipclient/data_endpointgrouprequest.go @@ -1,8 +1,21 @@ package dsipclient type EndpointGroupRequest struct { - Name string `json:"name"` - Calllimit string `json:"calllimit"` - Auth Auth `json:"auth"` - Endpoints []Endpoints `json:"endpoints"` + Name string `json:"name"` + CallSettings *CallSettings `json:"call_settings"` + Auth Auth `json:"auth"` + Endpoints []Endpoint `json:"endpoints"` + Strip int `json:"strip"` + Prefix string `json:"prefix"` + Notifications *NotificationSettings `json:"notifications"` +} + +type CallSettings struct { + Limit int `json:"limit"` + Timeout int `json:"timeout"` +} + +type NotificationSettings struct { + OverMaxCallLimit string `json:"overmaxcalllimit"` + EndpointFailure string `json:"endpointfailure"` } diff --git a/dsipclient/data_endpoints.go b/dsipclient/data_endpoints.go index 7ed4aa8..7b7a22a 100644 --- a/dsipclient/data_endpoints.go +++ b/dsipclient/data_endpoints.go @@ -1,7 +1,10 @@ package dsipclient -type Endpoints struct { - Hostname string `json:"hostname"` +type Endpoint struct { + Host string `json:"host"` + Port int `json:"port"` + Signalling string `json:"signalling"` + Media string `json:"media"` Description string `json:"description"` - Maintmode string `json:"maintmode"` + RWeight int `json:"rweight"` } diff --git a/dsiprouter.go b/dsiprouter.go index bc0a963..7630498 100644 --- a/dsiprouter.go +++ b/dsiprouter.go @@ -7,6 +7,8 @@ import ( "html" "os" "strings" + "encoding/json" + "flag" _ "github.com/go-sql-driver/mysql" ) @@ -81,18 +83,21 @@ func main() { switch os.Args[1] { - case "lease": + case "lease": + processCommandLease(dsipClient) - processCommandLease(dsipClient) + case "inboundmapping": + processCommandInboundMapping(dsipClient) - case "inboundmapping": - - processCommandInboundMapping(dsipClient) - - case "agent": - - processCommandAgent(dsipClient) + case "agent": + processCommandAgent(dsipClient) + case "endpointgroup": + processCommandEndpointGroup(dsipClient, os.Args[2:]) + + default: + fmt.Printf("Unknown command: %s\n", os.Args[1]) + os.Exit(1) } } func createConnection() *dsipclient.Client { @@ -156,6 +161,7 @@ func processCommandLease(dsipClient *dsipclient.Client) { fmt.Println("Arg(s):") fmt.Println("\temail\t\tA valid email address") fmt.Println("\tttl\t\tAmount of time for the SIP credentials to be available. 30 minute maximum for the Free plan") + fmt.Println("\tendpointgroup\t Manage Endpoint Groups via config or CLI flags") //fmt.Printf("\tsipdomain\tThe name of sipdomain . The default is %s for the Free plan\n", DEFAULT_SIP_DOMAIN) os.Exit(1) @@ -322,6 +328,60 @@ func processCommandAgent(dsipClient *dsipclient.Client) { } } +func processCommandEndpointGroup(dsipClient *dsipclient.Client, args []string) { + + // Set up flags + fs := flag.NewFlagSet("endpointgroup", flag.ExitOnError) + + // Define CLI flags + configFile := fs.String("file", "", "Path to JSON config file") + overrideName := fs.String("name", "", "Override endpoint group name") + overrideIP := fs.String("ip", "", "Override endpoint IP address") + overridePort := fs.Int("port", 0, "Override endpoint port") + + fs.Parse(args) + + if *configFile == "" { + fmt.Println("Usage: dsiprouter-cli endpointgroup --file=config.json [--name=...] [--ip=...] [--port=...]") + os.Exit(1) + } + + // Read the config file + data, err := os.ReadFile(*configFile) + if err != nil { + fmt.Printf("Error reading config file: %v\n", err) + os.Exit(1) + } + + // Parse JSON into request struct + var req dsipclient.EndpointGroupRequest + if err := json.Unmarshal(data, &req); err != nil { + fmt.Printf("Error parsing JSON: %v\n", err) + os.Exit(1) + } + + // Apply CLI overrides if present + if *overrideName != "" { + req.Name = *overrideName + } + if *overrideIP != "" && len(req.Endpoints) > 0 { + req.Endpoints[0].Host = *overrideIP + } + if *overridePort != 0 && len(req.Endpoints) > 0 { + req.Endpoints[0].Port = *overridePort + } + + // Call the API + resp, err := dsipClient.CreateEndpointGroup(&req) + if err != nil { + fmt.Printf("\033[31m Error: %v\033[0m\n", err) + os.Exit(1) + } + + fmt.Printf("Endpoint Group '%s' created for %s:%d — Response: %v\n", + req.Name, req.Endpoints[0].Host, req.Endpoints[0].Port, resp) +} + func getFreePBXDIDS() ([]DID, error) { var dids []DID