Skip to content
This repository was archived by the owner on Aug 6, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,40 @@ if err := page.Render("hackernews.png", "png", 100); err != nil {
You can also use the `RenderBase64()` to return a base64 encoded image to your
program instead of writing the file to disk.

### Via proxy

You can via proxy with `phantomjs.SetProxy` | `page.SetProxy` function or `page.SetSettings` with `proxy` omitempty.

Usage

```go
p := phantomjs.DefaultProcess
if err := p.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
defer phantomjs.DefaultProcess.Close()

// p.SetProxy(host_or_IP, port, proxy_type, user_name, password)
p.SetProxy("8.8.8.8", "8888", "socks5", "", "")

page, err := p.CreateWebPage()
if err != nil {
fmt.Println(err)
return
}
defer page.Close()

page.SetProxy("socks5://8.8.8.8:8888")

page.SetSettings(phantomjs.WebPageSettings{
Proxy: "socks5://8.8.8.8:8888",
})
```

priority order: `page.SetSettings` > `page.SetProxy` > `phantomjs.SetProxy`





49 changes: 49 additions & 0 deletions phantomjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ func (p *Process) Close() (err error) {
return err
}

// SetProxy set the process proxy
// host_or_IP, port, proxy_type, user_name, password
func (p *Process) SetProxy(hostOrIP, port, proxyType, userName, password string) (err error) {
if p.cmd == nil {
err = errors.New("process is closed")
return
}

req := map[string]interface{}{
"host_or_IP": hostOrIP,
"port": port,
"proxy_type": proxyType,
"user_name": userName,
"password": password,
}

if err = p.doJSON("POST", "/phantomjs/SetProxy", req, nil); err != nil {
return err
}

return
}

// URL returns the process' API URL.
func (p *Process) URL() string {
return fmt.Sprintf("http://localhost:%d", p.Port)
Expand Down Expand Up @@ -646,6 +669,7 @@ func (p *WebPage) Settings() (WebPageSettings, error) {
UserAgent: resp.Settings.UserAgent,
Username: resp.Settings.Username,
Password: resp.Settings.Password,
Proxy: resp.Settings.Proxy,
XSSAuditingEnabled: resp.Settings.XSSAuditingEnabled,
WebSecurityEnabled: resp.Settings.WebSecurityEnabled,
ResourceTimeout: time.Duration(resp.Settings.ResourceTimeout) * time.Millisecond,
Expand All @@ -666,6 +690,7 @@ func (p *WebPage) SetSettings(settings WebPageSettings) error {
UserAgent: settings.UserAgent,
Username: settings.Username,
Password: settings.Password,
Proxy: settings.Proxy,
XSSAuditingEnabled: settings.XSSAuditingEnabled,
WebSecurityEnabled: settings.WebSecurityEnabled,
ResourceTimeout: int(settings.ResourceTimeout / time.Millisecond),
Expand Down Expand Up @@ -713,6 +738,11 @@ func (p *WebPage) SetViewportSize(width, height int) error {
return p.ref.process.doJSON("POST", "/webpage/SetViewportSize", map[string]interface{}{"ref": p.ref.id, "width": width, "height": height}, nil)
}

// SetProxy sets the proxy of the web page.
func (p *WebPage) SetProxy(proxy string) error {
return p.ref.process.doJSON("POST", "/webpage/SetProxy", map[string]interface{}{"ref": p.ref.id, "proxy": proxy}, nil)
}

// WindowName returns the window name of the web page.
func (p *WebPage) WindowName() (string, error) {
var resp struct {
Expand Down Expand Up @@ -1129,6 +1159,7 @@ type WebPageSettings struct {
UserAgent string
Username string
Password string
Proxy string
XSSAuditingEnabled bool
WebSecurityEnabled bool
ResourceTimeout time.Duration
Expand All @@ -1141,6 +1172,7 @@ type webPageSettingsJSON struct {
UserAgent string `json:"userAgent"`
Username string `json:"username"`
Password string `json:"password"`
Proxy string `json:"proxy,omitempty"`
XSSAuditingEnabled bool `json:"XSSAuditingEnabled"`
WebSecurityEnabled bool `json:"webSecurityEnabled"`
ResourceTimeout int `json:"resourceTimeout"`
Expand All @@ -1161,6 +1193,7 @@ var server = webserver.create();
server.listen(system.env["PORT"], function(request, response) {
try {
switch (request.url) {
case '/phantomjs/SetProxy': return handlePhantomjsSetProxy(request, response);
case '/ping': return handlePing(request, response);
case '/webpage/CanGoBack': return handleWebpageCanGoBack(request, response);
case '/webpage/CanGoForward': return handleWebpageCanGoForward(request, response);
Expand Down Expand Up @@ -1203,6 +1236,7 @@ server.listen(system.env["PORT"], function(request, response) {
case '/webpage/URL': return handleWebpageURL(request, response);
case '/webpage/ViewportSize': return handleWebpageViewportSize(request, response);
case '/webpage/SetViewportSize': return handleWebpageSetViewportSize(request, response);
case '/webpage/SetProxy': return handleWebpageSetProxy(request, response);
case '/webpage/WindowName': return handleWebpageWindowName(request, response);
case '/webpage/ZoomFactor': return handleWebpageZoomFactor(request, response);
case '/webpage/SetZoomFactor': return handleWebpageSetZoomFactor(request, response);
Expand Down Expand Up @@ -1243,6 +1277,13 @@ server.listen(system.env["PORT"], function(request, response) {
}
});

function handlePhantomjsSetProxy(request, response) {
var msg = JSON.parse(request.post)
phantom.setProxy(msg.host_or_IP, msg.port, msg.proxy_type, msg.user_name, msg.password);
response.write(JSON.stringify({}));
response.closeGracefully();
}

function handlePing(request, response) {
response.statusCode = 200;
response.write('ok');
Expand Down Expand Up @@ -1532,6 +1573,14 @@ function handleWebpageSetViewportSize(request, response) {
response.closeGracefully();
}

function handleWebpageSetProxy(request, response) {
var msg = JSON.parse(request.post);
var page = ref(msg.ref);
page.setProxy(msg.proxy);
response.write(JSON.stringify({}));
response.closeGracefully();
}

function handleWebpageWindowName(request, response) {
var page = ref(JSON.parse(request.post).ref);
response.write(JSON.stringify({value: page.windowName}));
Expand Down
2 changes: 1 addition & 1 deletion phantomjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/benbjohnson/phantomjs"
"github.com/ego008/gophantomjs"
)

// Ensure web page can return whether it can navigate forward.
Expand Down