|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strconv" |
| 12 | + |
| 13 | + "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + //argument flags |
| 22 | + filePtr *string |
| 23 | + outputTextPtr *bool |
| 24 | + serverModePtr *int |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + initFlags() |
| 29 | + initLogger() |
| 30 | + |
| 31 | + serverMode := isFlagPassed("p") |
| 32 | + |
| 33 | + if serverMode { |
| 34 | + serveApi() |
| 35 | + } else { // things to pluck passed via a yaml file |
| 36 | + pluckFromFile() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/* Server Mode |
| 41 | +Listens on a port and answers online queries of type: |
| 42 | +http://localhost:8080?baseUrl="example.com"&xpath="/html/body"®ex="" |
| 43 | +*/ |
| 44 | +func serveApi() { |
| 45 | + logIt("Started HTTP server on localhost: "+strconv.Itoa(*serverModePtr), true) |
| 46 | + |
| 47 | + http.HandleFunc("/", handleHttp) |
| 48 | + fmt.Println(http.ListenAndServe(":"+strconv.Itoa(*serverModePtr), nil)) |
| 49 | +} |
| 50 | + |
| 51 | +func handleHttp(w http.ResponseWriter, req *http.Request) { |
| 52 | + results := make(map[string]string) |
| 53 | + req.ParseForm() |
| 54 | + baseUrl := req.Form.Get("baseUrl") |
| 55 | + xpath := req.Form.Get("xpath") |
| 56 | + regex := req.Form.Get("regex") |
| 57 | + |
| 58 | + results["baseUrl"] = baseUrl |
| 59 | + results["xpath"] = xpath |
| 60 | + results["regex"] = regex |
| 61 | + |
| 62 | + logIt(getIp(req) + " " + req.Header.Get("User-Agent") + " Request: ") |
| 63 | + logIt(results) |
| 64 | + defer func() { // in case of panic |
| 65 | + if err := recover(); err != nil { |
| 66 | + http.Error(w, "my own error message", http.StatusInternalServerError) |
| 67 | + fmt.Fprintf(w, "Webpluck encountered an error. Make sure that the baseUrl is a valid URL and xpath and regex are valid\n") |
| 68 | + fmt.Fprintf(w, "Error encountered is:\n%s\n", err) |
| 69 | + logIt(err) |
| 70 | + } |
| 71 | + }() |
| 72 | + text := ExtractTextFromUrl(baseUrl, xpath, regex) |
| 73 | + results["pluckedData"] = text |
| 74 | + jsonString, err := json.MarshalIndent(results, "", " ") |
| 75 | + check(err) |
| 76 | + fmt.Fprintf(w, string(jsonString)) |
| 77 | + logIt("Answer: " + text) |
| 78 | +} |
| 79 | + |
| 80 | +func pluckFromFile() { |
| 81 | + data, err := ioutil.ReadFile(*filePtr) |
| 82 | + check(err) |
| 83 | + var list targetList |
| 84 | + |
| 85 | + err = yaml.Unmarshal(data, &list) |
| 86 | + check(err) |
| 87 | + |
| 88 | + results := make(map[string]string) |
| 89 | + |
| 90 | + for _, t := range list.TargetList { |
| 91 | + text := ExtractTextFromUrl(t.BaseUrl, t.Xpath, t.Regex) |
| 92 | + results[t.Name] = text |
| 93 | + if *outputTextPtr { // if output to text (t) flag is set |
| 94 | + fmt.Println(t.Name + ": " + text) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + logIt("Webpluck invoked. Reading from file: " + *filePtr) |
| 99 | + logIt(results) |
| 100 | + |
| 101 | + if !*outputTextPtr { // default case is to print in JSON |
| 102 | + jsonString, err := json.MarshalIndent(results, "", " ") |
| 103 | + check(err) |
| 104 | + fmt.Println(string(jsonString)) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func isFlagPassed(name string) bool { |
| 109 | + found := false |
| 110 | + flag.Visit(func(f *flag.Flag) { |
| 111 | + if f.Name == name { |
| 112 | + found = true |
| 113 | + } |
| 114 | + }) |
| 115 | + return found |
| 116 | +} |
| 117 | + |
| 118 | +/* Parse command line flage and initiaize the global flag variables */ |
| 119 | +func initFlags() { |
| 120 | + flag.Usage = func() { |
| 121 | + fmt.Fprintln(os.Stderr, "---------------------------------------------------------") |
| 122 | + fmt.Fprintln(os.Stderr, "Usage: $ ./webpluck [-f filename.yml]") |
| 123 | + fmt.Fprintln(os.Stderr, "Example: $ ./webpluck -f ./extract_list.yml") |
| 124 | + fmt.Fprintln(os.Stderr, "---------------------------------------------------------\nFlags:") |
| 125 | + flag.PrintDefaults() |
| 126 | + } |
| 127 | + |
| 128 | + filePtr = flag.String("f", "./targets.yml", |
| 129 | + "`File name (yml)` with the list of targets to pluck/extract") |
| 130 | + outputTextPtr = flag.Bool("t", false, |
| 131 | + "Outputs the results in `text format` instead of JSON (applicable only in non server mode)") |
| 132 | + serverModePtr = flag.Int("p", 0, |
| 133 | + "`Port number` to serve webpluck as in HTTP API") |
| 134 | + flag.Parse() |
| 135 | +} |
| 136 | + |
| 137 | +func check(e error) { |
| 138 | + if e != nil { |
| 139 | + panic(e) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Get IP address of the incoming HTTP request based on forwarded-for |
| 144 | +// header (present in case of proxy). If not, use the remote address |
| 145 | +func getIp(req *http.Request) string { |
| 146 | + forwarded := req.Header.Get("X-FORWARDED-FOR") |
| 147 | + var addr string |
| 148 | + if forwarded != "" { |
| 149 | + addr = forwarded |
| 150 | + } |
| 151 | + addr = req.RemoteAddr |
| 152 | + ip, _, _ := net.SplitHostPort(addr) |
| 153 | + return ip |
| 154 | +} |
0 commit comments