http apis

  1. http.Get(url string) (resp *Response, err error)
  2. http.Head(url string) (resp *Response, err error)
  3. http.Post(url string, bodyType string, body io.Reader) (resp *Response, err error)

POST

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. )
  9. func main() {
  10. form := url.Values{}
  11. form.Add("username","srat")
  12. form.Add("password","123456")
  13. url := "http://xxx/login"
  14. contentType := "application/x-www-form-urlencoded"
  15. if res,err := http.Post(url,contentType,strings.NewReader(form.Encode()));err!=nil{
  16. log.Fatal("conn err")
  17. defer res.Body.Close()
  18. }else {
  19. resp,_ := ioutil.Readall(res.Body)
  20. fmt.Println(string())
  21. }
  22. }

或者

  1. form := url.Values{}
  2. form.Add("username","srat")
  3. form.Add("password","123456")
  4. url := "http://xxx/login"
  5. //contentType := "application/x-www-form-urlencoded"
  6. if res,err := http.PostForm(url,form);err!=nil{
  7. log.Fatal("conn err")
  8. }else {
  9. resp,_ := ioutil.Readall(res.Body)
  10. fmt.Println(string(resp))
  11. }

Using Structured Response Parsing

response结构体

  1. type Response struct {
  2. Status string // e.g. "200 OK"
  3. StatusCode int // e.g. 200
  4. Proto string // e.g. "HTTP/1.0"
  5. ProtoMajor int // e.g. 1
  6. ProtoMinor int // e.g. 0
  7. Header Header
  8. Body io.ReadCloser
  9. ContentLength int64
  10. TransferEncoding []string
  11. Close bool
  12. Uncompressed bool
  13. Trailer Header
  14. Request *Request
  15. TLS *tls.ConnectionState
  16. }

解析json数据

解析Fofa返回json

  1. package main
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. )
  11. type info struct {
  12. Mode string `json:"mode"` // tag 即解析json的别名
  13. Error bool `json:"error"`
  14. Query string `json:"query"`
  15. Page int `json:"page"`
  16. Size int `json:"size"`
  17. Results [][]string `json:"results"`
  18. }
  19. var (
  20. key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  21. mail = "xxxxxxxxxxxxxxx"
  22. query string
  23. )
  24. func main() {
  25. a := []byte("domain=nuist.edu.cn")
  26. query = base64.StdEncoding.EncodeToString(a) // base64
  27. url := fmt.Sprintf("https://fofa.so/api/v1/search/all?email=%s&key=%s&qbase64=%s",mail,key,query)
  28. res,err := http.Get(url)
  29. var i info
  30. if err != nil{
  31. log.Fatal(err)
  32. os.Exit(0)
  33. }else {
  34. if resp,err := ioutil.ReadAll(res.Body);err!=nil{
  35. log.Fatal("err")
  36. os.Exit(0)
  37. }else {
  38. fmt.Println(string(resp))
  39. if err = json.Unmarshal(resp,&i);err!=nil{ // json反序列化
  40. fmt.Println(err)
  41. os.Exit(0)
  42. }
  43. }
  44. results :=i.Results
  45. for _,v :=range (results){
  46. fmt.Println(v[0])
  47. }
  48. }
  49. }

image.png
这里需要注意的点比较多。声名接收json反序列化结构体的时候,成员变量首字符要大写,否则就会出现反序列化赋值无法成功。还有就是Unmarshal接收参数为指针,不能直接传入变量实体。这是个非常简单的Demo版本,好在Fofa的一切查询都是基于语法,后期想要修改也比较容易。