func ConfBridgeChannelRedirect(sess *agi.Session) { confno, err := sess.GetVariable("CONFNO") if err != nil { LoggerErr(err) } else { if confno.Dat == sess.Env["callerid"] { bridgepeer, err := sess.GetVariable("BRIDGEPEER") if err != nil { LoggerErr(err) } var rc1 = make(map[string]string) rc1["Channel"] = sess.Env["channel"] rc1["Exten"] = confno.Dat rc1["Context"] = CONFBRIDGE_CONTEXT var rc2 = make(map[string]string) rc2["Channel"] = bridgepeer.Dat rc2["Exten"] = confno.Dat rc2["Context"] = CONFBRIDGE_CONTEXT ConfBridgeSettings(sess.Env["uniqueid"]) RedirectChan(rc1) RedirectChan(rc2) LoggerString(fmt.Sprintf("Try create Confbridge CONFNO %s Channel1 %s Channel2 %s", confno.Dat, sess.Env["channel"], bridgepeer.Dat)) } } }
func BlockedFromPSTN(sess *agi.Session) { dbinfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", DBHost, DBPort, DBUser, DBPass, DBName, DBSSL) db, err := sql.Open(DBType, dbinfo) if err != nil { LoggerErr(err) } rows, err := db.Query(fmt.Sprintf(BLOCKPSTNQUERY, sess.Env["callerid"])) if err != nil { LoggerErr(err) } defer rows.Close() var arg1, arg2 string for rows.Next() { rows.Scan(&arg1, &arg2) } db.Close() if len(arg1) != 0 && len(arg2) != 0 { _, err := sess.Exec("UserEvent", fmt.Sprintf(UEBLOCKEDABON, sess.Env["callerid"], sess.Env["uniqueid"])) if err != nil { LoggerErr(err) } sess.Hangup() } }
//test func checkIP(ipip string, sess *agi.Session) { useragent, err := sess.GetVariable("CHANNEL(useragent)") if err != nil { LoggerErr(err) } sipuri, err := sess.GetVariable("SIPURI") if err != nil { LoggerErr(err) } anet := false cip := net.ParseIP(ipip) for _, iprange := range ALLOW { ip, ipnet, err := net.ParseCIDR(iprange) if err != nil { LoggerErr(err) } for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { if ip.String() == cip.String() { LoggerString("IP FROM ALLOW NETWORK " + ip.String()) msg := fmt.Sprintf("User Agent: %s\nSIPURI: %s\nExtension: %s\nCallerid: %s", useragent.Dat, sipuri.Dat, sess.Env["extension"], sess.Env["callerid"]) NotifyTG(msg) anet = true return } } } if anet == false { whoisIP(ipip) } }
func Gegensprechanlage(sess *agi.Session) { tf := timeFormat2() rf := fmt.Sprintf("%s%s_%s_%s%s", DFVAR0[0], tf, sess.Env[DFVAR1[0]], sess.Env[DFVAR1[1]], DFVAR0[1]) _, err := sess.Exec(DFVAR2, rf) if err != nil { LoggerErr(err) } }
func eBackground(sess *agi.Session, dir string, phrases []string) { for _, phrase := range phrases { sess.Verbose("Phrase: " + phrase) _, err := sess.Exec("Background", fmt.Sprintf("%s%s", dir, phrase)) if err != nil { LoggerErr(err) } } }
func SetNY(sess *agi.Session) { NYG = arrayShuffle(NYG) f := strings.Split(NYG[0], ".") find := false for _, g := range f { if g == NYVAR1 { find = true } } if find == true { _, err := sess.Exec(NYVAR2, fmt.Sprintf("%s%s", NYVAR0[1], f[0])) if err != nil { LoggerErr(err) } } }
func QueuePrio(sess *agi.Session) { var r = make(map[string]string) r[AMIVAR0[0]] = AMIVAR0[1] r[AMIVAR0[1]] = AMIVARUE0 v6, err := sess.GetVariable(CUSTVARA6) v7, err := sess.GetVariable(CUSTVARA7) v8, err := sess.GetVariable(CUSTVARA8) if err != nil { LoggerErr(err) } r[AMIVARQ0[0]] = AMIVARQ0[1] r[AMIVARQ1] = sess.Env[CUSTVARC1] r[AMIVARQ2] = sess.Env[CUSTVARC2] r[AMIVARQ3] = sess.Env[CUSTVARC3] r[AMIVARQ6] = v6.Dat r[AMIVARQ7] = v7.Dat r[AMIVARQ8] = v8.Dat LoggerMap(r) amiAction(r) }
func BanIpFromPSTN(sess *agi.Session) { useragent, err := sess.GetVariable("CHANNEL(useragent)") if err != nil { LoggerErr(err) } else { LoggerString("UserAgent " + useragent.Dat) } sipuri, err := sess.GetVariable("SIPURI") if err != nil { LoggerErr(err) } else { LoggerString("SIPURI " + sipuri.Dat) } var BAN = make(map[string]string) rex, err := regexp.Compile(`^sip:(\S*)\@(\S*)\:(\S*)$`) res := rex.FindStringSubmatch(sipuri.Dat) if res != nil { BAN["num"] = res[1] BAN["ip"] = res[2] BAN["port"] = res[3] } rex1, err := regexp.Compile(`^sip:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$`) res1 := rex1.FindStringSubmatch(sipuri.Dat) if res1 != nil { BAN["num"] = "" BAN["ip"] = res1[1] BAN["port"] = "" } rex2, err := regexp.Compile(`^sip:(\S*)\@(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$`) res2 := rex2.FindStringSubmatch(sipuri.Dat) if res2 != nil { BAN["num"] = res2[1] BAN["ip"] = res2[2] BAN["port"] = "" } LoggerMap(BAN) if err != nil { LoggerString("ERR") } checkIP(BAN["ip"], sess) }
func CustomerGet(sess *agi.Session, cc string) ([]CustomerJ, error) { var q string if cc == "p" { q = fmt.Sprintf(CUSTURLQ0[0], sess.Env[CUSTVARC0]) } else if cc == "c" { c, err := sess.GetVariable(CUSTVARA3) if err != nil { LoggerErr(err) } else { LoggerString(c.Dat) } q = fmt.Sprintf(CUSTURLQ0[1], c.Dat) } rsp, err := http.NewRequest(CUSTURL2[0], fmt.Sprintf(CUSTURL2[1], q), nil) rq, err := http.DefaultClient.Do(rsp) if err != nil { return nil, err } defer rq.Body.Close() body, err := ioutil.ReadAll(rq.Body) line := strings.Replace(string(body), CUSTVARR0[0], CUSTVARR0[1], -1) line = strings.Replace(string(line), CUSTVARR1[0], CUSTVARR1[1], -1) line = strings.Replace(string(line), CUSTVARR2[0], CUSTVARR2[1], -1) line = strings.Replace(string(line), CUSTVARR3[0], CUSTVARR3[1], -1) line = strings.Replace(string(line), CUSTVARR4[0], CUSTVARR4[1], -1) line = strings.Replace(string(line), CUSTVARR5[0], CUSTVARR5[1], -1) line = strings.Replace(string(line), CUSTVARR6[0], CUSTVARR6[1], -1) line = strings.Replace(string(line), CUSTVARR7[0], CUSTVARR7[1], -1) line = strings.Replace(string(line), CUSTVARR8[0], CUSTVARR8[1], -1) var customerJ []CustomerJ err = json.Unmarshal([]byte(line), &customerJ) if err != nil { return nil, err } return customerJ, nil }
func agiSess(sess *agi.Session) { var err error startvar, err := sess.GetVariable("STARTVAR") if err == nil { switch startvar.Dat { case "inbound": InboundCall(sess) case "block": BanIpFromPSTN(sess) case "confbridge_access": ConfBridgeAccess(sess) case "confbridge_channelredirect": ConfBridgeChannelRedirect(sess) case "confbridge_confs": ConfBridgeConfs(sess) case "callback_call": CallbackCheck(sess) case "fax_receive": FaxRecv(sess) case "blocked_from_pstn": BlockedFromPSTN(sess) case "balance": BalanceInfo(sess) case "target_call": TargetCall(sess) case "call1": Call1(sess) case "customer_get_test_0": CustomerCheck(sess, "p") case "customer_get_test_1": CustomerCheck(sess, "c") case "queue_prio": QueuePrio(sess) case "ny": SetNY(sess) case "df": Gegensprechanlage(sess) default: sess.Verbose("DEFAULT STARTVAR") } } sess.Verbose("================== Complete ======================") sess.Verbose("STARTVAR IS " + startvar.Dat) return }
func InboundCall(sess *agi.Session) { // LoggerString("INCOMING NUM " + sess.Env["callerid"]) rex, err := regexp.Compile(`^[7|8](\d{10})$`) res := rex.FindStringSubmatch(sess.Env["callerid"]) if res != nil { LoggerString("RES NOT NIL " + sess.Env["callerid"]) _, err := sess.SetVariable("CALLERID(num)", res[1]) if err != nil { LoggerErr(err) } else { LoggerString("NUM CHANGED TO " + res[1]) } _, err = sess.SetVariable("CALLERID(name)", res[1]) if err != nil { LoggerErr(err) } } else { // LoggerString("NUM NOT CHANGED " + sess.Env["callerid"]) } if err != nil { LoggerErr(err) } rex2, err := regexp.Compile(`^([a|A]nonymous|unknown)$`) res2 := rex2.FindStringSubmatch(sess.Env["calleridname"]) if res2 != nil { LoggerString("RES2 " + res2[1]) _, err := sess.SetVariable("CALLERID(name)", "0") if err != nil { LoggerErr(err) } else { LoggerString("NAME CHANGED TO 0") } } rex3, err := regexp.Compile(`^([a|A]nonymous|unknown)$`) res3 := rex3.FindStringSubmatch(sess.Env["callerid"]) if res3 != nil { LoggerString("RES3 " + res3[1]) _, err := sess.SetVariable("CALLERID(num)", "0") if err != nil { LoggerErr(err) } else { LoggerString("NUM CHANGED TO 0") } } }
func ConfBridgeConfs(sess *agi.Session) { _, err := sess.Exec("DumpChan", "255") if err != nil { LoggerErr(err) } _, err = sess.SetVariable("__CONFNO", sess.Env["extension"]) if err != nil { LoggerErr(err) } _, err = sess.Exec("ConfBridge", fmt.Sprintf("%s,,,%s", sess.Env["extension"], UMENU)) if err != nil { LoggerErr(err) } LoggerString("Confbridge Admin add " + sess.Env["extension"]) }
func FaxRecv(sess *agi.Session) { sess.Answer() uid := strings.Split(sess.Env["uniqueid"], ".") _, err := sess.SetVariable("FAXFILENAME", fmt.Sprintf("%s_%s_%s", sess.Env["callerid"], sess.Env["dnid"], uid[0])) _, err = sess.SetVariable("FAXOPT(headerinfo)", fmt.Sprintf("Received_by_%s_%s", sess.Env["callerid"], uid[0])) _, err = sess.SetVariable("FAXOPT(localstationid)", sess.Env["callerid"]) _, err = sess.SetVariable("FAXOPT(maxrate)", "14400") _, err = sess.SetVariable("FAXOPT(minrate)", "4800") filename, err := sess.GetVariable("FAXFILENAME") _, err = sess.Exec("ReceiveFax", fmt.Sprintf(FAXDIR+FAXRECVSTR, filename.Dat)) if err != nil { LoggerErr(err) } else { fs, err := sess.GetVariable("FAXSTATUS") fp, err := sess.GetVariable("FAXPAGES") fb, err := sess.GetVariable("FAXBITRATE") fr, err := sess.GetVariable("FAXRESOLUTION") if err != nil { LoggerErr(err) } if fs.Dat == "" || fs.Dat == "FAILED" { fs.Dat = "FAILED" } msg := fmt.Sprintf("Статус: %s\nС номера: %s\nНа номер: %s\nКоличество страниц: %s\nСкорость передачи(bitrate): %s\nРазрешение файла: %s", fs.Dat, sess.Env["callerid"], sess.Env["dnid"], fp.Dat, fb.Dat, fr.Dat) NotifyMail("ФаксВходящий", sess.Env["callerid"], msg, MAIL) NotifyMail("ФаксВходящий", sess.Env["callerid"], msg, "fax-"+sess.Env["dnid"]) } sess.Hangup() }
func BalanceInfo(sess *agi.Session) { b, err := sess.GetVariable(BALANCE) if err != nil { LoggerErr(err) } else { LoggerString(b.Dat) } c, err := sess.GetVariable(BALCONTRACT) if err != nil { LoggerErr(err) } sess.Verbose(fmt.Sprintf("BALANCE: %s %s %s", b.Dat, c.Dat, sess.Env["callerid"])) LoggerString(fmt.Sprintf("BALANCE: %s %s %s", b.Dat, c.Dat, sess.Env["callerid"])) bi, err := strconv.Atoi(b.Dat) sess.Verbose("BALANCE TO INT") bif, err := strconv.ParseFloat(b.Dat, 64) sess.Verbose(bi) sess.Verbose(bif) FILES := make([]string, 0) if bif < 0 { sess.Verbose("< 0") FILES = []string{BALNOMONEY, BALONCONRACT, BALMINUS} eBackground(sess, BALDIR, FILES) /* } else if bif > 0 && bif < 500 { sess.Verbose("0 > < 500") FILES = []string{BALONCONRACT} eBackground(sess, BALDIR, FILES) } else if bif > 500 { sess.Verbose("> 500") FILES = []string{BALRICH, BALONCONRACT} eBackground(sess, BALDIR, FILES) */ } else { sess.Verbose("ELSE") FILES = []string{BALONCONRACT} eBackground(sess, BALDIR, FILES) } bii := strconv.FormatFloat(bif, 'g', 10, 64) rex, err := regexp.Compile(`^(\d+)\.(\d{2})\d*$`) res := rex.FindStringSubmatch(bii) if res != nil { sess.Verbose(res[1] + " " + res[2]) rub := res[1] kop := res[2] rex2, err := regexp.Compile(`^0(\d+)$`) res2 := rex2.FindStringSubmatch(kop) if res2 != nil { kop = res2[1] } if err != nil { LoggerErr(err) } BalanceDigits(sess, BALRUB, rub) BalanceDigits(sess, BALKOP, kop) } rex3, err := regexp.Compile(`^(\d+)\.(\d{1})$`) res3 := rex3.FindStringSubmatch(bii) if res3 != nil { sess.Verbose(res3[1] + " " + res3[2]) rub := res3[1] kop := res3[2] BalanceDigits(sess, BALRUB, rub) BalanceDigits(sess, BALKOP, kop) } rex4, err := regexp.Compile(`^(\d+)$`) res4 := rex4.FindStringSubmatch(bii) if res4 != nil { sess.Verbose(res4[1]) rub := res4[1] if rub == "0" { FILES = []string{"0", BALRUB + "-i", "0", BALKOP + "-k"} eBackground(sess, BALDDIR, FILES) } else { BalanceDigits(sess, BALRUB, rub) } } rex5, err := regexp.Compile(`^\-(\d+)\.(\d{2})\d*$`) res5 := rex5.FindStringSubmatch(bii) if res5 != nil { sess.Verbose(res5[1] + " " + res5[2]) rub := res5[1] kop := res5[2] rex55, err := regexp.Compile(`^0(\d+)$`) res55 := rex55.FindStringSubmatch(kop) if res55 != nil { kop = res55[1] } if err != nil { LoggerErr(err) } BalanceDigits(sess, BALRUB, rub) BalanceDigits(sess, BALKOP, kop) } rex6, err := regexp.Compile(`^\-(\d+)$`) res6 := rex6.FindStringSubmatch(bii) if res6 != nil { sess.Verbose(res6[1]) rub := res6[1] if rub == "0" { FILES = []string{"0", BALRUB + "-i", "0", BALKOP + "-k"} eBackground(sess, BALDDIR, FILES) } else { BalanceDigits(sess, BALRUB, rub) } } }
func CustomerCheck(sess *agi.Session, cc string) { CSTMR := make(map[int][]map[string]string) customer, err := CustomerGet(sess, cc) if err != nil { fmt.Println(err.Error()) } else { i := 0 for _, cust := range customer { cstmr := make(map[string]string) if cust.Var6 == "2" && cust.Var3 == "0" { if sess.Env[CUSTVARC0] != cust.Var7[1:] { LoggerString(fmt.Sprintf("%s != %s %s", sess.Env[CUSTVARC0], cust.Var7[1:], "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")) } cstmr[CUSTVARR0[0]] = cust.Var0 cstmr[CUSTVARR1[0]] = cust.Var1 cstmr[CUSTVARR2[0]] = cust.Var2 cstmr[CUSTVARR3[0]] = cust.Var3 cstmr[CUSTVARR4[0]] = cust.Var4 cstmr[CUSTVARR5[0]] = cust.Var5 cstmr[CUSTVARR6[0]] = cust.Var6 cstmr[CUSTVARR7[0]] = cust.Var7 cstmr[CUSTVARR8[0]] = cust.Var8 LoggerString("\n" + CUSTVARC0 + " " + sess.Env[CUSTVARC0] + "\n" + CUSTVARR0[0] + " " + cust.Var0 + "\n" + CUSTVARR1[0] + " " + cust.Var1 + "\n" + CUSTVARR2[0] + " " + cust.Var2 + "\n" + CUSTVARR3[0] + " " + cust.Var3 + "\n" + CUSTVARR4[0] + " " + cust.Var4 + "\n" + CUSTVARR5[0] + " " + cust.Var5 + "\n" + CUSTVARR6[0] + " " + cust.Var6 + "\n" + CUSTVARR7[0] + " " + cust.Var7[1:] + "\n" + CUSTVARR8[0] + " " + cust.Var8 + "\n") CSTMR[i] = append(CSTMR[i], cstmr) i++ } } ii := strconv.Itoa(i) if i == 1 { /* _, err := sess.SetVariable(CUSTVARA0, ii) */ _, err = sess.SetVariable(CUSTVARA1, CSTMR[0][0][CUSTVARR1[0]]) /* _, err = sess.SetVariable(CUSTVARA2, CSTMR[0][0][CUSTVARR2[0]]) _, err = sess.SetVariable(CUSTVARA5, CSTMR[0][0][CUSTVARR5[0]]) */ if err != nil { LoggerErr(err) } LoggerString("\n===================================================\n" + CUSTVARA0 + " " + ii + "\n" + CUSTVARA1 + " " + CSTMR[0][0][CUSTVARR1[0]] + "\n" + CUSTVARA2 + " " + CSTMR[0][0][CUSTVARR2[0]] + "\n" + CUSTVARA5 + " " + CSTMR[0][0][CUSTVARR5[0]] + "\n" + CUSTVARR0[0] + " Count: " + ii + "\n" + "===================================================\n") } else if i > 1 { /* _, err := sess.SetVariable(CUSTVARA0, ii) if err != nil { LoggerErr(err) } */ LoggerString("\n===================================================\n") LoggerString(CUSTVARA0 + " " + ii) /* for _, vr := range CSTMR { LoggerString("++++++!!!!!!!!!!!!!!!!") // LoggerMap2(vr) if vr[0][CUSTVARR5[0]] == "1" { LoggerString(vr[0][CUSTVARR5[0]]) _, err = sess.SetVariable(CUSTVARA5, CSTMR[0][0][CUSTVARR5[0]]) if err != nil { LoggerErr(err) } } LoggerString("++++++!!!!!!!!!!!!!!!!") } */ LoggerString(CUSTVARR0[0] + " Count: " + ii + "\n" + "===================================================\n") } else { LoggerString("\n===================================================\n" + CUSTVARC0 + " " + sess.Env[CUSTVARC0] + "\n" + CUSTVARR0[0] + " Count: " + ii + "\n" + "===================================================\n") /* _, err = sess.SetVariable(CUSTVARA0, "0") */ _, err = sess.SetVariable(CUSTVARA1, "0") /* _, err = sess.SetVariable(CUSTVARA2, "N") _, err = sess.SetVariable(CUSTVARA5, "N") */ if err != nil { LoggerErr(err) } } } }
func ConfBridgeAccess(sess *agi.Session) { sess.Answer() _, err := sess.SetVariable("__CONFNO", sess.Env["extension"]) if err != nil { LoggerErr(err) } _, err = sess.SetVariable("__DYNAMIC_FEATURES", CONFBRIDGE_FEATURES) if err != nil { LoggerErr(err) } if sess.Env["extension"] == sess.Env["callerid"] { inner_num, err := strconv.Atoi(LEN_INNER_NUM) if len(sess.Env["callerid"]) == inner_num { _, err = sess.Exec("ConfBridge", fmt.Sprintf("%s,,,%s", sess.Env["extension"], AMENU)) } else { _, err = sess.Exec("ConfBridge", fmt.Sprintf("%s,,,%s", sess.Env["extension"], UMENU)) } if err != nil { LoggerErr(err) } } else { _, err = sess.Exec("ConfBridge", fmt.Sprintf("%s,,,%s", sess.Env["extension"], UMENU)) } if err != nil { LoggerErr(err) } LoggerString("Confbridge Admin " + sess.Env["extension"]) }