Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyonc committed Mar 2, 2024
1 parent 8719969 commit eeba52a
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 69 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<img src="/resources/icon.png" alt="[logo]" width="48"/> sysproxy
<img src="/resources/Icon.png" alt="[logo]" width="48"/> sysproxy
=======================
### What it is
Windows system proxy forward tool that display as tray menu
Expand Down Expand Up @@ -28,6 +28,12 @@ Download the compiled version from [release page](https://github.com/zhyonc/sysp
- Click Log menu item to open log form
- If everything is ok, you can see the connection status in the log form
![Outbound](/resources/Outbound.png)
#### Proxy Toggle
Click the tray icon to enable/disable proxy
- <img src="/resources/Icon.png" alt="[Icon]" width="16" height="16"/> Both inbound and outbound is disabled
- <img src="/resources/IconI.png" alt="[IconI]" width="16" height="16"/> Only inbound is enabled
- <img src="/resources/IconO.png" alt="[IconO]" width="16" height="16"/> Only outbound is enabled
- <img src="/resources/IconIO.png" alt="[IconIO]" width="16" height="16"/> Both inbound and outbound is enabled
### Build
- Upgrade go version to above 1.9.2
- Download package tool from [rsrc](https://github.com/akavel/rsrc/releases)
Expand Down
6 changes: 2 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
appName string = "sysproxy"
version string = "0.1.1"
version string = "0.2.0"
)

var path string = "config.toml"
Expand All @@ -31,7 +31,7 @@ func NewConfig() *Config {
}
}
if conf.Menu.Version < version {
upgradeConfig(conf)
conf = upgradeConfig(conf)
}
return conf
}
Expand All @@ -46,7 +46,6 @@ func defualtConfig() *Config {
InboundCheckedIndex: 0,
OutboundCheckedIndex: 0,
AutoStart: false,
AutoProxy: false,
},
}
return conf
Expand All @@ -57,7 +56,6 @@ func upgradeConfig(oldConf *Config) *Config {
newConf.OutboundList = oldConf.OutboundList
newConf.InboundList = oldConf.InboundList
newConf.Menu.AutoStart = oldConf.Menu.AutoStart
newConf.Menu.AutoProxy = oldConf.Menu.AutoProxy
ok := newConf.Save()
if !ok {
log.Println("update config failed")
Expand Down
1 change: 0 additions & 1 deletion config/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ type Menu struct {
Version string
OutboundCheckedIndex int
InboundCheckedIndex int
AutoProxy bool
AutoStart bool
}
1 change: 1 addition & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type MenuController interface {
SwitchInbound(index int) error
ToggleAutoStart() error
OpenAboutURL()
SaveConfig() bool
Exit() string
}

Expand Down
43 changes: 20 additions & 23 deletions controller/menu_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,41 @@ func (c *menuController) GetInboundTags() []string {

// SwitchOutbound implements MenuController.
func (c *menuController) SwitchOutbound(index int) error {
c.conf.Menu.OutboundCheckedIndex = index // for auto proxy when restart
if index < 0 || index > len(c.conf.OutboundList) {
return fmt.Errorf("can't find outbound at %d", index)
}
if c.service != nil {
c.service.StopService()
}
if index == 0 {
c.service = nil
return nil
}
if index < 1 || index > len(c.conf.OutboundList) {
return fmt.Errorf("can't find outbound at %d", index)
}
outbound := c.conf.OutboundList[index-1] // outbound list don't contain disable tag
if outbound.SrcProto == service.PAC || outbound.SrcProto == service.HTTP {
err := service.PACService.CreatePACTempFile(outbound)
if err != nil {
return err
if index > 0 {
outbound := c.conf.OutboundList[index-1] // OutboundList don't include disable tag
if outbound.SrcProto == service.PAC || outbound.SrcProto == service.HTTP {
err := service.PACService.CreatePACTempFile(outbound)
if err != nil {
return err
}
c.service = service.NewHttpService(outbound)
} else if outbound.SrcProto == service.SOCKS5 {
c.service = service.NewSocks5Service(outbound)
} else {
return fmt.Errorf("unknown src protocol at %d", index)
}
c.service = service.NewHttpService(outbound)
} else if outbound.SrcProto == service.SOCKS5 {
c.service = service.NewSocks5Service(outbound)
} else {
return fmt.Errorf("unknown src protocol at %d", index)
go c.service.StartService()
}
go c.service.StartService()
return nil
}

// SwitchInbound implements MenuController.
func (c *menuController) SwitchInbound(index int) error {
c.conf.Menu.InboundCheckedIndex = index // for auto proxy when restart
if index < 0 || index > len(c.conf.InboundList) {
return fmt.Errorf("can't find inbound at %d", index)
}
if index == 0 {
util.ClearProxy()
return nil
}
if index < 1 || index > len(c.conf.InboundList) {
return fmt.Errorf("can't find inbound at %d", index)
}
inbound := c.conf.InboundList[index-1] // inbound list don't contain disable tag
inbound := c.conf.InboundList[index-1] // InboundList don't include disable tag
switch inbound.DstProto {
case service.PAC:
util.EnablePAC(inbound.DstIP, inbound.DstPort)
Expand Down
Binary file added resources/Icon.ico
Binary file not shown.
Binary file added resources/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/Icon.psd
Binary file not shown.
Binary file added resources/IconI.ico
Binary file not shown.
Binary file added resources/IconI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/IconIO.ico
Binary file not shown.
Binary file added resources/IconIO.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/IconO.ico
Binary file not shown.
Binary file added resources/IconO.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ import (
_ "embed"
)

//go:embed icon.ico
//go:embed Icon.ico
var IconData []byte

//go:embed IconI.ico
var IconIData []byte

//go:embed IconO.ico
var IconOData []byte

//go:embed IconIO.ico
var IconIOData []byte

//go:embed abp.js
var AbpData []byte

Expand Down
3 changes: 2 additions & 1 deletion service/base_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ func (s *baseService) disconnect(conn net.Conn) {

func (s *baseService) disconnectAll() {
s.conns.Range(func(key, _ any) bool {
s.conns.Delete(key)
conn, ok := key.(net.Conn)
if !ok {
log.Printf("Error disconnect all because dessert net.Conn failed")
return true
} else {
err := conn.Close()
if err != nil {
log.Printf("Error disconnect: %v", err)
}
}
s.conns.Delete(key)
return true
})
}
Expand Down
2 changes: 1 addition & 1 deletion util/regedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func HookAutoStart(checked bool, appName string) error {
if err != nil {
return err
}
run := "cmd /c cd " + filepath.Dir(ex) + " && start " + filepath.Base(ex)
run := "cmd /c " + filepath.VolumeName(ex) + " && cd " + filepath.Dir(ex) + " && start " + filepath.Base(ex)
if checked {
// Set the application to auto start
if err := key.SetStringValue(appName, run); err != nil {
Expand Down
109 changes: 72 additions & 37 deletions view/menu_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
)

type menuView struct {
controller controller.MenuController
logView View
ruleView View
inboundView BoundView
outboundView BoundView
popupMenu *vcl.TPopupMenu
controller controller.MenuController
logView View
ruleView View
inboundView BoundView
outboundView BoundView
trayIcon *vcl.TTrayIcon
popupMenu *vcl.TPopupMenu
isDisabled bool
outboundIndexCache int
inboundIndexCache int
}

func NewMenuView(controller controller.MenuController, logView, ruleView View, inboundView, outboundView BoundView) *menuView {
Expand All @@ -29,7 +33,9 @@ func NewMenuView(controller controller.MenuController, logView, ruleView View, i
appName := v.controller.GetMenu().AppName
v.inboundView.SetRefreshTagsFunc(v.onRefreshTags)
v.outboundView.SetRefreshTagsFunc(v.onRefreshTags)
v.drawPopupMenu(newTrayIcon(newMainForm(), appName))
v.trayIcon = newTrayIcon(newMainForm(), appName)
v.trayIcon.SetOnClick(v.onTrayIconClick)
v.drawPopupMenu()
return v
}

Expand All @@ -53,8 +59,26 @@ func newTrayIcon(form *vcl.TForm, appName string) *vcl.TTrayIcon {
return trayIcon
}

func (v *menuView) drawPopupMenu(trayIcon *vcl.TTrayIcon) {
v.popupMenu = vcl.NewPopupMenu(trayIcon)
func (v *menuView) onTrayIconClick(sender vcl.IObject) {
menu := v.controller.GetMenu()
var outboundIndex int = 0
var inboundIndex int = 0
if v.isDisabled {
outboundIndex = v.outboundIndexCache
inboundIndex = v.inboundIndexCache
} else {
v.outboundIndexCache = menu.OutboundCheckedIndex
v.inboundIndexCache = menu.InboundCheckedIndex
}
outbound := vcl.AsMenuItem(v.popupMenu.FindComponent(outboundName))
inbound := vcl.AsMenuItem(v.popupMenu.FindComponent(inboundName))
outbound.Items(int32(outboundIndex)).Click()
inbound.Items(int32(inboundIndex)).Click()
v.isDisabled = !v.isDisabled
}

func (v *menuView) drawPopupMenu() {
v.popupMenu = vcl.NewPopupMenu(v.trayIcon)
var index int = 0
v.popupMenu.Items().Add(newMenuItem(v.popupMenu, &index, outboundName, outboundText, nil))
v.popupMenu.Items().Add(newMenuItem(v.popupMenu, &index, inboundName, inboundText, nil))
Expand All @@ -63,8 +87,8 @@ func (v *menuView) drawPopupMenu(trayIcon *vcl.TTrayIcon) {
v.popupMenu.Items().Add(v.newSettingMenuItem(v.popupMenu, &index, settingName, settingText, nil))
v.popupMenu.Items().Add(newMenuItem(v.popupMenu, &index, aboutName, aboutText, v.onAboutMenuItemClick))
v.popupMenu.Items().Add(newMenuItem(v.popupMenu, &index, exitName, exitText, v.onExitMenuItemClick))
trayIcon.SetPopupMenu(v.popupMenu)
trayIcon.SetVisible(true)
v.trayIcon.SetPopupMenu(v.popupMenu)
v.trayIcon.SetVisible(true)
}

func (v *menuView) newSettingMenuItem(parent vcl.IComponent, index *int, name, title string, clickEvent vcl.TNotifyEvent) *vcl.TMenuItem {
Expand All @@ -73,9 +97,6 @@ func (v *menuView) newSettingMenuItem(parent vcl.IComponent, index *int, name, t
settingMenuItem.Add(newMenuItem(settingMenuItem, &subIndex, settingOutboundName, settingOutboundText, v.showForm))
settingMenuItem.Add(newMenuItem(settingMenuItem, &subIndex, settingInboundName, settingInboundText, v.showForm))
menu := v.controller.GetMenu()
autoProxyMenuItem := newMenuItem(settingMenuItem, &subIndex, settingAutoProxyName, settingAutoProxyText, v.onSettingAutoProxyClick)
autoProxyMenuItem.SetChecked(menu.AutoProxy)
settingMenuItem.Add(autoProxyMenuItem)
autoStartMenuItem := newMenuItem(settingMenuItem, &subIndex, settingAutoStartName, settingAutoStartText, v.onSettingAutoStartClick)
autoStartMenuItem.SetChecked(menu.AutoStart)
settingMenuItem.Add(autoStartMenuItem)
Expand All @@ -85,6 +106,10 @@ func (v *menuView) newSettingMenuItem(parent vcl.IComponent, index *int, name, t
func (v *menuView) Run() {
v.onRefreshTags(outboundName, v.controller.GetOutboundTags())
v.onRefreshTags(inboundName, v.controller.GetInboundTags())
menu := v.controller.GetMenu()
if menu.InboundCheckedIndex == 0 && menu.OutboundCheckedIndex == 0 {
v.isDisabled = true
}
vcl.Application.Run()
}

Expand All @@ -102,14 +127,6 @@ func (v *menuView) showForm(sender vcl.IObject) {
}
}

func (v *menuView) onSettingAutoProxyClick(sender vcl.IObject) {
menuItem := vcl.AsMenuItem(sender)
menu := v.controller.GetMenu()
checked := !menu.AutoProxy
menuItem.SetChecked(checked)
menu.AutoProxy = checked
}

func (v *menuView) onSettingAutoStartClick(sender vcl.IObject) {
menuItem := vcl.AsMenuItem(sender)
menu := v.controller.GetMenu()
Expand Down Expand Up @@ -159,22 +176,17 @@ func (v *menuView) onRefreshTags(name string, tags []string) {
boundMenuItem.Add(newMenuItem(boundMenuItem, &index, "", tag, v.onBoundSubMenuItemClick))
}
menu := v.controller.GetMenu()
if menu.AutoProxy {
var subMenuItem *vcl.TMenuItem
if boundMenuItem.Name() == outboundName {
subMenuItem = boundMenuItem.Items(int32(menu.OutboundCheckedIndex))
} else if boundMenuItem.Name() == inboundName {
subMenuItem = boundMenuItem.Items(int32(menu.InboundCheckedIndex))
} else {
subMenuItem = nil
}
if subMenuItem != nil {
subMenuItem.Click()
return
}
var subMenuItem *vcl.TMenuItem
if boundMenuItem.Name() == outboundName {
subMenuItem = boundMenuItem.Items(int32(menu.OutboundCheckedIndex))
} else if boundMenuItem.Name() == inboundName {
subMenuItem = boundMenuItem.Items(int32(menu.InboundCheckedIndex))
} else {
subMenuItem = nil
}
if subMenuItem != nil {
subMenuItem.Click()
}
disableMenuItem := boundMenuItem.Items(0)
disableMenuItem.Click()
}

func (v *menuView) onBoundSubMenuItemClick(sender vcl.IObject) {
Expand All @@ -200,4 +212,27 @@ func (v *menuView) onBoundSubMenuItemClick(sender vcl.IObject) {
item.SetChecked(false)
}
menuItem.SetChecked(true)
menu := v.controller.GetMenu()
if parentMenuItem.Name() == outboundName {
menu.OutboundCheckedIndex = menuItem.Tag()
} else if parentMenuItem.Name() == inboundName {
menu.InboundCheckedIndex = menuItem.Tag()
}
_ = v.controller.SaveConfig()
v.refreshIcon()
}

func (v *menuView) refreshIcon() {
menu := v.controller.GetMenu()
newIcon := vcl.NewIcon()
if menu.OutboundCheckedIndex == 0 && menu.InboundCheckedIndex == 0 {
newIcon.LoadFromBytes(resources.IconData)
} else if menu.OutboundCheckedIndex > 0 && menu.InboundCheckedIndex == 0 {
newIcon.LoadFromBytes(resources.IconOData)
} else if menu.OutboundCheckedIndex == 0 && menu.InboundCheckedIndex > 0 {
newIcon.LoadFromBytes(resources.IconIData)
} else {
newIcon.LoadFromBytes(resources.IconIOData)
}
v.trayIcon.SetIcon(newIcon)
}

0 comments on commit eeba52a

Please sign in to comment.