├── go.mod ├── main_test.go ├── README.md ├── main.go └── funcs └── funcs.go /go.mod: -------------------------------------------------------------------------------- 1 | module consulta-operadora 2 | 3 | go 1.19 4 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | /* ==> go test */ 2 | 3 | package main 4 | 5 | import ( 6 | "consulta-operadora/funcs" 7 | "fmt" 8 | "html" 9 | "os" 10 | "strings" 11 | "testing" 12 | ) 13 | 14 | // GetStr Returns empty string if no start string found 15 | func GetStr(str string, start string, end string) (result string, found bool) { 16 | s := strings.Index(str, start) 17 | if s == -1 { 18 | return result, false 19 | } 20 | newS := str[s+len(start):] 21 | e := strings.Index(newS, end) 22 | if e == -1 { 23 | return result, false 24 | } 25 | result = newS[:e] 26 | return result, true 27 | } 28 | 29 | func TestGetStr(t *testing.T) { 30 | body := `11` 31 | result, found := GetStr(body, ``, ``) 32 | if !found { 33 | t.Error("string not found") 34 | os.Exit(0) 35 | } 36 | t.Error(result) 37 | } 38 | 39 | func TestContains(t *testing.T) { 40 | str := `Aguarde` 41 | if strings.Contains(str, "Aguarde") { 42 | t.Error("found") 43 | } else { 44 | t.Error("not found") 45 | } 46 | } 47 | 48 | func TestChar(t *testing.T) { 49 | operadora := ` Telef�nica Brasil (Móvel/SMP)` 50 | portado := ` NÃO` 51 | remove_spaces_operadora := strings.TrimSpace(operadora) 52 | remove_html_entities_operadora := html.UnescapeString(remove_spaces_operadora) 53 | 54 | fmt.Println(remove_html_entities_operadora) 55 | fmt.Println("----") 56 | fmt.Println(strings.TrimSpace(html.UnescapeString(portado))) 57 | } 58 | 59 | func TestRegex(t *testing.T) { 60 | test := funcs.IsValidPhone("11975738658") 61 | fmt.Println(test) 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 | 5 | Logo 6 | 7 | 8 |

consulta-operadora

9 | 10 |

11 | 📞: consult the operator of a telephone in Brazil 🇧🇷 12 |
13 |
14 | issues 15 |

16 |
17 | 18 | 19 | 20 | ## Getting Started 21 | 22 | Follow the steps below and if you want to contribute to our project by opening a pull request. 23 | 24 | ### Prerequisites 25 | 26 | You need to install golang and run it from a cli (Command Line Application). 27 | 28 | - go 29 | ```sh 30 | go run main.go 31 | ``` 32 | 33 | ### Installation 34 | 35 | _Below is an example of how you can install and setting up the project._ 36 | 37 | 1. Install golang at [https://go.dev/dl/](https://go.dev/dl/) 38 | 2. Clone the repo 39 | ```sh 40 | git clone https://github.com/unkperson/consulta-operadora.git 41 | ``` 42 | 3. Enter valid cli arguments 43 | ```sh 44 | go run main.go -phone 000 45 | ``` 46 | 47 | 48 | 49 | ## Roadmap 50 | 51 | - [x] CLI 52 | - [x] version 53 | - [x] phone 54 | - [x] funcs 55 | - [x] README.md 56 | 57 | See the [open issues](https://github.com/unkperson/consulta-operadora/issues) for a full list of proposed features (and known issues). 58 | 59 | 60 | 61 | ## Contributing 62 | 63 | Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. 64 | 65 | If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". 66 | Don't forget to give the project a star! Thanks again! 67 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "consulta-operadora/funcs" 5 | "flag" 6 | "fmt" 7 | "html" 8 | "os" 9 | "strings" 10 | ) 11 | 12 | var logo = ` 13 | _ _ _ _ _ _ ___ ____ ____ ____ ____ _ _ 14 | | | |\ | |_/ |__] |___ |__/ [__ | | |\ | 15 | |__| | \| | \_ | |___ | \ ___] |__| | \| 16 | ` 17 | 18 | func init() { 19 | funcs.Message(logo, "purple") 20 | } 21 | 22 | func main() { 23 | var version bool 24 | 25 | flag.BoolVar(&version, "version", false, "Software version.") 26 | flag.BoolVar(&version, "v", false, "Software version.") 27 | phone := flag.String("phone", "", "Phone to search.") 28 | flag.Parse() 29 | 30 | if version { 31 | funcs.Message("[!] version 1.0", "white") 32 | os.Exit(0) 33 | } 34 | 35 | if *phone == "" { 36 | funcs.Message("[!] no phone number found", "red") 37 | os.Exit(0) 38 | } 39 | 40 | if !funcs.IsValidPhone(*phone) { 41 | funcs.Message("[!] enter a valid phone number", "red") 42 | os.Exit(0) 43 | } 44 | 45 | // do post request 46 | data, time, status := funcs.DoPost(*phone) 47 | 48 | // remove html entities 49 | data = html.UnescapeString(data) 50 | 51 | // check status code 52 | if status != 200 { 53 | funcs.Message("[!] statusCode is not 200", "red") 54 | os.Exit(0) 55 | } 56 | fmt.Println(fmt.Sprintf("req delay => %s", time)) 57 | 58 | if strings.Contains(data, "Aguarde") { 59 | time, _ := funcs.GetStr(data, ``, ``) 60 | funcs.Message("[!] wait "+time+"s and try again", "yellow") 61 | os.Exit(0) 62 | } 63 | 64 | operadora, _ := funcs.GetStr(data, `Operadora:`, ``) 65 | portado, _ := funcs.GetStr(data, `Portado:`, ``) 66 | 67 | // remove spaces from the beginning 68 | operadora = strings.TrimSpace(operadora) 69 | portado = strings.TrimSpace(portado) 70 | 71 | // to lower case 72 | operadora = strings.ToLower(operadora) 73 | portado = strings.ToLower(portado) 74 | 75 | funcs.Message("["+*phone+"] operadora: "+operadora+" - portado: "+portado, "green") 76 | } 77 | -------------------------------------------------------------------------------- /funcs/funcs.go: -------------------------------------------------------------------------------- 1 | package funcs 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "net/http" 8 | "net/url" 9 | "regexp" 10 | "strings" 11 | "time" 12 | ) 13 | 14 | var colors = map[string]string{ 15 | "default": "\033[0m", 16 | "red": "\033[31m", 17 | "green": "\033[32m", 18 | "yellow": "\033[33m", 19 | "blue": "\033[34m", 20 | "purple": "\033[35m", 21 | "white": "\033[97m", 22 | } 23 | 24 | func IsValidPhone(result string) bool { 25 | found, _ := regexp.MatchString(`^\(?\d{2}\)?[\s-]?[\s9]?\d{4}-?\d{4}$`, result) 26 | if found { 27 | return true 28 | } else { 29 | return false 30 | } 31 | } 32 | 33 | func Message(message, colorType string) { 34 | fmt.Println(colors[colorType] + message + colors["default"]) 35 | } 36 | 37 | func GetStr(str string, start string, end string) (result string, found bool) { 38 | s := strings.Index(str, start) 39 | if s == -1 { 40 | return result, false 41 | } 42 | newS := str[s+len(start):] 43 | e := strings.Index(newS, end) 44 | if e == -1 { 45 | return result, false 46 | } 47 | result = newS[:e] 48 | return result, true 49 | } 50 | 51 | func DoPost(phone string) (string, time.Duration, int) { 52 | start := time.Now() 53 | 54 | formData := url.Values{} 55 | formData.Set("tipo", "consulta") 56 | formData.Set("numero", phone) 57 | 58 | client := &http.Client{} 59 | 60 | req, err := http.NewRequest("POST", "http://consultaoperadora.com.br/site2015/resposta.php", strings.NewReader(formData.Encode())) 61 | if err != nil { 62 | log.Fatal(err) 63 | } 64 | 65 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 66 | 67 | resp, err := client.Do(req) 68 | if err != nil { 69 | log.Fatal(err) 70 | } 71 | defer resp.Body.Close() 72 | duration := time.Since(start) 73 | 74 | data, err := io.ReadAll(resp.Body) 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | 79 | return string(data), duration, resp.StatusCode 80 | } 81 | 82 | func DoGet() (string, time.Duration, int) { 83 | // get current time 84 | start := time.Now() 85 | 86 | resp, err := http.Get("........") 87 | if err != nil { 88 | log.Fatal(err) 89 | } 90 | defer resp.Body.Close() 91 | duration := time.Since(start) 92 | 93 | data, err := io.ReadAll(resp.Body) 94 | if err != nil { 95 | log.Fatal(err) 96 | } 97 | 98 | return string(data), duration, resp.StatusCode 99 | } 100 | --------------------------------------------------------------------------------