diff --git a/examples/main_freefb_client.go b/examples/main_freefb_client.go new file mode 100644 index 0000000..16b0a58 --- /dev/null +++ b/examples/main_freefb_client.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "github.com/joho/godotenv" + "github.com/1Conan/transports" + "os" +) + +func main() { + godotenv.Load() + + fmt.Println("Transports test") + + freefbTransport := transports.FreeFBTransport{ + Login: os.Getenv("FB_LOGIN"), + Password: os.Getenv("FB_PASSWORD"), + Friend: os.Getenv("FB_FRIEND"), + } + + Proxy := transports.Proxy{ + Transport: freefbTransport, + Port: 8080, + } + + Proxy.Listen() +} diff --git a/transport_freefb.go b/transport_freefb.go new file mode 100644 index 0000000..44a2563 --- /dev/null +++ b/transport_freefb.go @@ -0,0 +1,98 @@ +package transports + +import ( + "errors" + "fmt" + "github.com/headzoo/surf" + "github.com/headzoo/surf/browser" + "io/ioutil" + "net/http" +) + +type FreeFBTransport struct { + *Transport + Login string + Password string + Friend string + Browser *browser.Browser + Serializer DefaultSerializer + ChatUrl string +} + +func (t *FreeFBTransport) DoLogin() bool { + fmt.Println("FreeFBTransport, Login()") + err := t.Browser.Open("https://free.facebook.com/") + if err != nil { + panic(err) + } + + LoginForm := t.Browser.Forms()[1] + LoginForm.Input("email", t.Login) + LoginForm.Input("pass", t.Password) + if LoginForm.Submit() != nil { + panic(err) + } + + err = t.Browser.Open("https://free.facebook.com/profile.php") + + if err != nil { + panic(err) + } + + fmt.Println("Logged in as", t.Browser.Title(), "?") + + FriendUrl := fmt.Sprintf("https://free.facebook.com/%s", t.Friend) + err = t.Browser.Open(FriendUrl) + + if err != nil { + panic(err) + } + + t.Browser.Click("a[href*=\"/messages/thread/\"]") + + t.ChatUrl = t.Browser.Url().String() + + return true + +} + +func (t *FreeFBTransport) Prepare() { + fmt.Println("FreeFBTransport, Prepare()") + + t.Serializer = DefaultSerializer{} + + t.Browser = surf.NewBrowser() + + if !t.DoLogin() { + err := errors.New("Authentication error!") + panic(err) + } + + return +} + +func (t *FreeFBTransport) Handler(w http.ResponseWriter, originalRequest *http.Request) { + + t.Browser.Open(t.ChatUrl) + + client := &http.Client{} + + request, _ := http.NewRequest(originalRequest.Method, originalRequest.URL.String(), nil) + + serializedRequest := t.Serializer.Serialize(request, true).([]byte) + + fmt.Println("Got", originalRequest) + fmt.Println("Serialized", string(serializedRequest)) + + MessageForm, _ := t.Browser.Form("#composer_form") + MessageForm.Input("body", string(serializedRequest)) + MessageForm.Submit() + + resp, _ := client.Do(request) + b, _ := ioutil.ReadAll(resp.Body) + resp.Body.Close() + + w.Write(b) + + return +}