Skip to content

Commit

Permalink
0.4.3: cmdline mode, switch of proxying setting
Browse files Browse the repository at this point in the history
  • Loading branch information
yinghuocho committed Jun 4, 2016
1 parent d513e23 commit 89e69ad
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
4 changes: 2 additions & 2 deletions client/Makefile
@@ -1,5 +1,5 @@
VERSION := 0.4.2
SHORTVERSION := 0.4.2
VERSION := 0.4.3
SHORTVERSION := 0.4.3

.PHONY: windows package-windows linux package-linux darwin package-darwin clean

Expand Down
86 changes: 59 additions & 27 deletions client/main.go
Expand Up @@ -34,7 +34,7 @@ import (
)

const (
FIREFLY_VERSION = "0.4.2"
FIREFLY_VERSION = "0.4.3"
)

type clientOptions struct {
Expand All @@ -49,6 +49,7 @@ type clientOptions struct {
localHTTPAddr string
localUIAddr string
trackingID string
cmdMode bool
}

type fireflyClient struct {
Expand Down Expand Up @@ -247,6 +248,20 @@ func (c *fireflyClient) stopAutoUpdate() bool {
return false
}

func (c *fireflyClient) setPAC() bool {
if c.options.cmdMode {
return false
}

if c.appData != nil {
v, ok := c.appData.Get("setPAC")
if ok && v == "0" {
return false
}
}
return true
}

func (c *fireflyClient) startUpdater() {
proxyURL, _ := url.Parse("http://" + c.httpListener.Addr().String())
privKey, e := c.loadUpdateKey()
Expand Down Expand Up @@ -357,7 +372,9 @@ func (c *fireflyClient) changeLocale(locale string) {
c.appData.Put("locale", locale)
}
c.configureI18n()
c.reloadSystray()
if !c.options.cmdMode {
c.reloadSystray()
}
}

func (c *fireflyClient) exit(err error) {
Expand Down Expand Up @@ -401,7 +418,8 @@ func (c *fireflyClient) handleSignals() {
}()
}

func (c *fireflyClient) _main() {
func (c *fireflyClient) parseOptions() {
flag.BoolVar(&c.options.cmdMode, "cmd-mode", false, "command line mode")
flag.StringVar(&c.options.localSocksAddr, "local-socks-addr", "127.0.0.1:38250", "SOCKS proxy address")
flag.StringVar(&c.options.localHTTPAddr, "local-http-addr", "127.0.0.1:38251", "HTTP proxy address")
flag.StringVar(&c.options.localUIAddr, "local-ui-addr", "127.0.0.1:38252", "Web UI address, use random local address when specified address is not available")
Expand All @@ -414,7 +432,9 @@ func (c *fireflyClient) _main() {
flag.StringVar(&c.options.updateURL, "update-url", "https://update.gofirefly.org/update", "url for auto-update")
flag.StringVar(&c.options.trackingID, "tracking-id", "UA-76209591-1", "Google Analytics tracking ID")
flag.Parse()
}

func (c *fireflyClient) _main() {
var err error
c.fs, err = tarfs.New(Resources, "")
if err != nil {
Expand Down Expand Up @@ -546,44 +566,51 @@ func (c *fireflyClient) _main() {
}
}
c.ui = startUI(c, uiListener)
log.Printf("settings url: %s", c.ui.settingsUrl)
// see ui.go
go c.uiCommandProc()

// set PAC
icon, err := c.fs.Get("icons/24.ico")
if err != nil {
log.Fatalf("Unable to load icon for PAC: %s", err)
}
err = promptPrivilegeEscalation(icon)
if err != nil {
log.Fatalf("Unable to escalate priviledge for setting PAC: %s", err)
if !c.options.cmdMode && c.setPAC() {
// set PAC
icon, err := c.fs.Get("icons/24.ico")
if err != nil {
log.Fatalf("Unable to load icon for PAC: %s", err)
}
err = promptPrivilegeEscalation(icon)
if err != nil {
log.Fatalf("Unable to escalate priviledge for setting PAC: %s", err)
}
pacURL := c.ui.handle(pacFilename(), pacHandler(c.httpListener.Addr().String()))
enablePAC(pacURL)
c.addExitFunc(func() {
disablePAC(pacURL)
})
}
pacURL := c.ui.handle(pacFilename(), pacHandler(c.httpListener.Addr().String()))
enablePAC(pacURL)
c.addExitFunc(func() {
disablePAC(pacURL)
})

// systray
c.addExitFunc(systray.Quit)
c.configureSystray()
if !c.options.cmdMode {
c.addExitFunc(systray.Quit)
c.configureSystray()
}

// clean exit with signals
go c.handleSignals()

// pid file
utils.SavePid(c.options.pidFilename)

// open starting pages
if c.openSettingsPage() {
c.ui.show()
}
if c.openLandingPage() {
if !c.options.cmdMode {
// open starting pages
if c.openSettingsPage() {
// wait to avoid launching new browser window
time.Sleep(3 * time.Second)
c.ui.show()
}
if c.openLandingPage() {
if c.openSettingsPage() {
// wait to avoid launching new browser window
time.Sleep(3 * time.Second)
}
c.ui.open(c.options.landingPage)
}
c.ui.open(c.options.landingPage)
}

// updater
Expand All @@ -606,5 +633,10 @@ func main() {
exitCh: make(chan error, 1),
chExitFuncs: make(chan func(), 10),
}
systray.Run(client._main)
client.parseOptions()
if !client.options.cmdMode {
systray.Run(client._main)
} else {
client._main()
}
}
3 changes: 2 additions & 1 deletion client/resources/locale/en_US.json
Expand Up @@ -12,7 +12,8 @@
"UI_ADDITIONAL_DOMAINS": "Additional websites(domains) you want to tunnel through proxy",
"UI_SETTINGS_PAGE": "Open (this) settings page when Firefly starts",
"UI_LANDING_PAGE": "Open embedded landing page when Firefly starts",
"UI_STOP_AUTOUPDATE": "Stop automatically updating to latest version",
"UI_SET_PAC": "Try to set browser proxy when FIrefly starts",
"UI_STOP_AUTOUPDATE": "Stop automatically updating to the latest version",
"UI_FEEDBACK": "Issues/Feedback",
"UI_CLICK_LINK": "Please click this link",
"UI_LANG": "Language",
Expand Down
1 change: 1 addition & 0 deletions client/resources/locale/zh_CN.json
Expand Up @@ -12,6 +12,7 @@
"UI_ADDITIONAL_DOMAINS": "其他通过代理转发的网站(域名)",
"UI_SETTINGS_PAGE": "Firefly 启动时自动打开配置界面(即本页面)",
"UI_LANDING_PAGE": "Firefly 启动时自动打开内置首页",
"UI_SET_PAC": "Firefly 启动时自动设置浏览器代理",
"UI_STOP_AUTOUPDATE": "停止自动版本更新",
"UI_FEEDBACK": "问题/反馈",
"UI_CLICK_LINK": "请打开这个链接",
Expand Down
5 changes: 5 additions & 0 deletions client/resources/ui.html
Expand Up @@ -57,6 +57,11 @@ <h2>{{ i18n "UI_SETTINGS" }}</h2>
{{ i18n "UI_LANDING_PAGE" }}: <a href="{{ .LandingPage }}" target="_blank">{{ .LandingPage }}</a>
</label></li>

<li><label>
<input type="checkbox" id="setPAC"{{ if .SetPAC }} checked {{ end }}>
{{ i18n "UI_SET_PAC" }}
</label></li>

<li><label>
<input type="checkbox" id="stopAutoUpdate"{{ if .StopAutoUpdate }} checked {{ end }}>
{{ i18n "UI_STOP_AUTOUPDATE" }}
Expand Down
21 changes: 21 additions & 0 deletions client/ui.go
Expand Up @@ -118,6 +118,7 @@ type fireflySettings struct {
TunnellingAll bool
OpenSettingsPage bool
OpenLandingPage bool
SetPAC bool
StopAutoUpdate bool
Locales map[string]string
CurrentLocale string
Expand All @@ -143,6 +144,7 @@ func (u *fireflyUI) settingsGET(w http.ResponseWriter, req *http.Request) {
TunnellingAll: u.client.uiTunnellingAll(),
OpenSettingsPage: u.client.uiOpenSettingsPage(),
OpenLandingPage: u.client.uiOpenLandingPage(),
SetPAC: u.client.uiSetPAC(),
StopAutoUpdate: u.client.uiStopAutoUpdate(),
CustomTunnellingDomains: u.client.uiCustomTunnellingDomains(),
Locales: locales,
Expand Down Expand Up @@ -186,6 +188,12 @@ func (u *fireflyUI) settingsPOST(w http.ResponseWriter, req *http.Request) {
} else {
u.client.uiCommand(&uiCmd{cmd: "stopAutoUpdateOff"})
}
case "setPAC":
if state == "1" {
u.client.uiCommand(&uiCmd{cmd: "setPACOn"})
} else {
u.client.uiCommand(&uiCmd{cmd: "setPACOff"})
}
case "updateCustomTunnellingDomains":
args := []string{}
raw := strings.Split(state, "\n")
Expand Down Expand Up @@ -222,6 +230,13 @@ func (c *fireflyClient) uiVersion() string {
return v.(string)
}

func (c *fireflyClient) uiSetPAC() bool {
cmd := &uiCmd{cmd: "setPAC?", ret: make(chan interface{})}
c.uiCh <- cmd
v := <-cmd.ret
return v.(bool)
}

func (c *fireflyClient) uiTunnellingAll() bool {
cmd := &uiCmd{cmd: "tunnellingAll?", ret: make(chan interface{})}
c.uiCh <- cmd
Expand Down Expand Up @@ -298,6 +313,10 @@ func (c *fireflyClient) uiCommandProc() {
c.switchFlags("openLandingPage", true)
case cmd.cmd == "openLandingPageOff":
c.switchFlags("openLandingPage", false)
case cmd.cmd == "setPACOn":
c.switchFlags("setPAC", true)
case cmd.cmd == "setPACOff":
c.switchFlags("setPAC", false)
case cmd.cmd == "tunnellingAllOn":
c.switchTunnelling(true)
case cmd.cmd == "tunnellingAllOff":
Expand Down Expand Up @@ -325,6 +344,8 @@ func (c *fireflyClient) uiCommandProc() {
cmd.ret <- c.openSettingsPage()
case cmd.cmd == "openLandingPage?":
cmd.ret <- c.openLandingPage()
case cmd.cmd == "setPAC?":
cmd.ret <- c.setPAC()
case cmd.cmd == "stopAutoUpdate?":
cmd.ret <- c.stopAutoUpdate()
case cmd.cmd == "currentLocale":
Expand Down

0 comments on commit 89e69ad

Please sign in to comment.