Adrià Vilanova MartÃnez | 32758f0 | 2022-01-20 16:03:00 +0100 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "regexp" |
| 10 | "strconv" |
| 11 | "time" |
| 12 | |
| 13 | hguclient "gomodules.avm99963.com/hgu-sipsettings-reverter/client" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | obProxyAddrRe = regexp.MustCompile(`oa0 = "([\d\.]*)";`) |
| 18 | obProxyPortRe = regexp.MustCompile(`op0 = "(\d*)";`) |
| 19 | ipInterfaceRe = regexp.MustCompile(`var ifn = '([^']*)';`) |
| 20 | ) |
| 21 | |
| 22 | type OutboundProxySettings struct { |
| 23 | Address string |
| 24 | Port int |
| 25 | IpInterface string |
| 26 | } |
| 27 | |
| 28 | func GetOutboundProxySettings(client *hguclient.Client) (*OutboundProxySettings, error) { |
| 29 | loggedIn, err := client.IsLoggedIn() |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | if !loggedIn { |
| 34 | log.Println("Not logged in, logging in...") |
| 35 | err := client.LogIn() |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | } |
| 40 | body, err := client.Get(hguclient.URL_SIPBASIC, false) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | addrMatches := obProxyAddrRe.FindStringSubmatch(body) |
| 45 | if len(addrMatches) < 2 { |
| 46 | return nil, errors.New("Can't find outbound proxy address in settings.") |
| 47 | } |
| 48 | addr := addrMatches[1] |
| 49 | portMatches := obProxyPortRe.FindStringSubmatch(body) |
| 50 | if len(portMatches) < 2 { |
| 51 | return nil, errors.New("Can't find oubound proxy port in settings.") |
| 52 | } |
| 53 | port, err := strconv.Atoi(portMatches[1]) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | ipMatches := ipInterfaceRe.FindStringSubmatch(body) |
| 58 | if len(ipMatches) < 2 { |
| 59 | return nil, errors.New("Can't find IP interface in settings.") |
| 60 | } |
| 61 | ip := ipMatches[1] |
| 62 | return &OutboundProxySettings{ |
| 63 | Address: addr, |
| 64 | Port: port, |
| 65 | IpInterface: ip, |
| 66 | }, nil |
| 67 | } |
| 68 | |
| 69 | func check(client *hguclient.Client, correctSettings *OutboundProxySettings) error { |
| 70 | currentSettings, err := GetOutboundProxySettings(client) |
| 71 | if err != nil { |
| 72 | log.Fatalln(err) |
| 73 | } |
| 74 | if *currentSettings == *correctSettings { |
| 75 | log.Println("Settings are ok.") |
| 76 | return nil |
| 77 | } |
| 78 | log.Println(fmt.Sprintf("Settings are incorrect! Current settings: %v. Correct settings: %v.", currentSettings, correctSettings)) |
| 79 | _, err = client.Get("/voicesipstop.cmd", false) |
| 80 | if err != nil { |
| 81 | return errors.New(fmt.Sprintf("Couldn't stop SIP: %v", err)) |
| 82 | } |
| 83 | eAddr := url.QueryEscape(correctSettings.Address) |
| 84 | ePort := url.QueryEscape(strconv.Itoa(correctSettings.Port)) |
| 85 | eInt := url.QueryEscape(correctSettings.IpInterface) |
| 86 | startUrl := "/voicesipstart.cmd?currentview=basic&ifName=" + eInt + "&obProxyAddr0=" + eAddr + "&obProxyPort0=" + ePort |
| 87 | _, err = client.Get(startUrl, false) |
| 88 | if err != nil { |
| 89 | return errors.New(fmt.Sprintf("Couldn't start SIP: %v", err)) |
| 90 | } |
| 91 | log.Println("Settings have been fixed") |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func mustGetEnv(name string) string { |
| 96 | env, ok := os.LookupEnv(name) |
| 97 | if !ok { |
| 98 | log.Fatalln(fmt.Sprintf("%v environment variable must be set.", name)) |
| 99 | } |
| 100 | return env |
| 101 | } |
| 102 | |
| 103 | func main() { |
| 104 | log.Println("Starting sip_reverter") |
| 105 | |
| 106 | endpoint := mustGetEnv("SIPREVERTER_HGU_ENDPOINT") |
| 107 | password := mustGetEnv("SIPREVERTER_HGU_PASSWORD") |
| 108 | |
| 109 | address := mustGetEnv("SIPREVERTER_CONF_ADDRESS") |
| 110 | portString := mustGetEnv("SIPREVERTER_CONF_PORT") |
| 111 | port, err := strconv.Atoi(portString) |
| 112 | if err != nil { |
| 113 | log.Fatalln(fmt.Sprintf("Can't parse port: %v", err)) |
| 114 | } |
| 115 | ipInterface := mustGetEnv("SIPREVERTER_CONF_INTERFACE") |
| 116 | correctObProxySettings := &OutboundProxySettings{ |
| 117 | Address: address, |
| 118 | Port: port, |
| 119 | IpInterface: ipInterface, |
| 120 | } |
| 121 | |
| 122 | client, err := hguclient.New(endpoint, password) |
| 123 | if err != nil { |
| 124 | log.Fatalln(err) |
| 125 | } |
| 126 | |
| 127 | for { |
| 128 | err := check(client, correctObProxySettings) |
| 129 | if err != nil { |
| 130 | log.Println(fmt.Sprintf("Error while checking: %v", err)) |
| 131 | } |
| 132 | time.Sleep(90 * time.Second) |
| 133 | } |
| 134 | } |