tools
/
wi
1
0
Fork 0
wi/main.go

150 lines
3.3 KiB
Go
Raw Permalink Normal View History

2016-07-21 19:40:19 +00:00
/**
* @file main.go
* @author Mikhail Klementyev jollheef<AT>riseup.net
* @license GNU GPLv3
* @date July, 2016
* @brief Tiny non-interactive cli browser
*/
2016-07-19 22:08:19 +00:00
package main
import (
2019-06-20 16:20:51 +00:00
"context"
"net/http"
2016-11-20 14:57:27 +00:00
"os"
2016-07-30 08:05:50 +00:00
"strings"
2019-06-20 16:29:08 +00:00
"code.dumpstack.io/tools/wi/commands"
"code.dumpstack.io/tools/wi/storage"
2016-07-22 12:43:23 +00:00
2016-11-20 14:57:27 +00:00
cookiejar "github.com/juju/persistent-cookiejar"
2016-07-19 22:08:19 +00:00
kingpin "gopkg.in/alecthomas/kingpin.v2"
2019-06-20 16:20:51 +00:00
"github.com/cretz/bine/process"
"github.com/cretz/bine/tor"
"github.com/ipsn/go-libtor"
2016-07-19 22:08:19 +00:00
)
2019-06-20 16:20:51 +00:00
var creator = libtor.Creator
type LibTorWrapper struct{}
func (LibTorWrapper) New(ctx context.Context, args ...string) (process.Process, error) {
return creator.New(ctx, args...)
}
2016-07-30 08:05:50 +00:00
type searchList []string
func (l *searchList) Set(value string) (err error) {
*l = append(*l, value)
return
}
func (l *searchList) String() (s string) {
return ""
}
func (l *searchList) IsCumulative() bool {
return true
}
func SearchList(settings kingpin.Settings) (target *[]string) {
target = new([]string)
settings.SetValue((*searchList)(target))
return
}
2016-07-19 22:08:19 +00:00
var (
2019-06-20 16:20:51 +00:00
useTor = kingpin.Flag("tor", "Use embedded tor").Default("false").Bool()
ua = kingpin.Flag("ua", "User-Agent").Default("Wi 0.1").String()
2016-07-24 08:01:55 +00:00
get = kingpin.Command("get", "Get url")
getUrl = get.Arg("url", "Url").Required().String()
2016-11-20 10:22:00 +00:00
form = kingpin.Command("form", "Fill form")
formID = form.Arg("id", "Form ID").Required().Int64()
formArgs = SearchList(form.Arg("args", "Form arguments"))
2016-08-01 00:11:48 +00:00
2016-07-25 15:41:12 +00:00
link = kingpin.Command("link", "Get link")
linkNo = link.Arg("no", "Number").Required().Int64()
linkFromHistory = link.Flag("history", "Item from history").Bool()
2016-07-24 08:01:55 +00:00
historyList = kingpin.Command("history", "List history")
historyListItems = historyList.Arg("items", "Amount of items").Int64()
2016-07-24 08:23:24 +00:00
historyListAll = historyList.Flag("all", "Show all items").Bool()
2016-07-30 08:05:50 +00:00
search = kingpin.Command("search", "Search by duckduckgo")
2016-07-30 08:05:50 +00:00
searchArgs = SearchList(search.Arg("string", "String for search"))
2016-07-19 22:08:19 +00:00
)
2016-07-22 12:43:23 +00:00
func main() {
2016-11-20 15:09:02 +00:00
homePath, exists := os.LookupEnv("HOME")
var wiDir, widbPath, wijarPath string
if exists {
wiDir = homePath + "/.wi"
widbPath = wiDir + "/wi.db"
wijarPath = wiDir + "/wi.jar"
} else {
wiDir = "/tmp"
widbPath = "/tmp/wi.db"
wijarPath = "/tmp/wi.jar"
}
err := os.MkdirAll(wiDir, 0700)
if err != nil {
panic(err)
}
db, err := storage.OpenDB(widbPath)
2016-07-22 12:43:23 +00:00
if err != nil {
panic(err)
}
2016-07-24 08:01:55 +00:00
defer db.Close()
2016-11-20 15:09:02 +00:00
os.Setenv("GOCOOKIES", wijarPath)
2016-11-20 14:57:27 +00:00
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
defer jar.Save()
2019-06-20 16:20:51 +00:00
kingpin.Parse()
var t *tor.Tor
if *useTor {
t, err = tor.Start(nil, &tor.StartConf{
ProcessCreator: LibTorWrapper{},
DataDir: wiDir + "/tor",
})
if err != nil {
panic(err)
}
defer t.Close()
dialer, err := t.Dialer(nil, nil)
if err != nil {
panic(err)
}
commands.Transport = &http.Transport{DialContext: dialer.DialContext}
}
commands.UserAgent = *ua
2016-07-24 08:01:55 +00:00
switch kingpin.Parse() {
case "get":
2016-11-20 14:57:27 +00:00
commands.Get(db, jar, *getUrl)
2016-11-20 10:22:00 +00:00
case "form":
2016-11-20 14:57:27 +00:00
commands.Form(db, jar, *formID, *formArgs)
2016-07-24 08:01:55 +00:00
case "link":
2016-11-20 14:57:27 +00:00
commands.Link(db, jar, *linkNo, *linkFromHistory)
2016-07-24 08:01:55 +00:00
case "history":
2016-07-25 15:35:09 +00:00
commands.History(db, *historyListItems, 20, *historyListAll)
2016-07-30 08:05:50 +00:00
case "search":
2017-10-05 06:44:20 +00:00
commands.Get(db, jar, "https://duckduckgo.com/html/?kd=-1&q="+strings.Join(*searchArgs, "+"))
2016-07-22 12:43:23 +00:00
}
}