예제 #1
0
func main() {
	username := flag.String("u", "", "shanxun username")
	password := flag.String("p", "", "shanxun password")
	isRouter := flag.Bool("r", false, "use router mode 2? (defalut off)")
	isDisconnect := flag.Bool("d", false, "disconnect?")
	realIP := flag.String("ip", "local", "real IP, 'stdin' or 'local' works router support [hiwifi openwrt asus]")
	raddress := flag.String("ra", "192.168.1.1", "router address")
	rusername := flag.String("ru", "root", "router username")
	rpassword := flag.String("rp", "admin", "router password")
	flag.Parse()
	config = &Config{*username, *password, *realIP, *raddress, *rusername, *rpassword}

	if !*isDisconnect {
		if config.Username == "" && !*isDisconnect {
			println(VERSION)
			println(EXAMPLE)
			flag.Usage()
			return
		} else if len(config.Username) < 12 || len(config.Password) != 6 {
			util.ERROR.Log("Invalid username or password")
			println(VERSION)
			println(EXAMPLE)
			flag.Usage()
			return
		}
	}
	d := netdialer.NewDialer(config.Username, config.Password)
	if config.RouterType != "" && config.RouterType != "local" {
		if config.RouterType == "stdin" {
			in := bufio.NewReader(os.Stdin)
			str, _ := in.ReadString(0)
			d.UserIP = str
		} else {
			d.UserIP = selectMode(config.RouterType)
		}
	}
	var err error
	if *isRouter {
		err = d.ConnectRouter()
	} else {
		if *isDisconnect {
			err = d.DisconnectDirect()
		} else {
			err = d.ConnectDirect()
		}
	}
	if err != nil {
		util.ERROR.Log(err)
	}
}
예제 #2
0
파일: gui.go 프로젝트: pa001024/netdialer
func main() {
	fout, _ := os.Create("dialer.log")
	defer fout.Close()
	bo := bufio.NewWriter(fout)
	defer bo.Flush()
	util.INFO.SetOutput(bo)
	util.ERROR.SetOutput(bo)

	bin, err := ioutil.ReadFile("config.json")
	if err == nil {
		json.Unmarshal(bin, config)
	} else {
		util.ERROR.Log(err)
		err = nil
	}

	var usr, pwd *walk.LineEdit
	var raddr, rusr, rpwd *walk.LineEdit
	var lb, rb *walk.PushButton
	var mode *walk.ComboBox
	var mw *walk.MainWindow
	var db *walk.DataBinder
	var laddr, lusr, lpwd *walk.Splitter
	go func() {
		for mw == nil {
			runtime.Gosched()
		}
		ic, err := walk.NewIconFromResourceId(6)
		if err == nil {
			// func onLoad() {
			db.SetAutoSubmit(true)
			mw.SetIcon(ic)
			switch config.RouterType {
			case "hiwifi":
				laddr.SetVisible(true)
				lusr.SetVisible(false)
				lpwd.SetVisible(true)
			case "openwrt":
				laddr.SetVisible(true)
				lusr.SetVisible(true)
				lpwd.SetVisible(true)
			case "asus":
				laddr.SetVisible(true)
				lusr.SetVisible(true)
				lpwd.SetVisible(true)
			default:
				laddr.SetVisible(false)
				lusr.SetVisible(false)
				lpwd.SetVisible(false)
			}
			mw.SetSize(walk.Size{0, 0})
			// }
		}
	}()
	MainWindow{
		AssignTo: &mw,
		Title:    TITLE,
		MinSize:  Size{340, 0},
		Layout:   VBox{},
		DataBinder: DataBinder{
			AssignTo:   &db,
			DataSource: config,
		},
		Children: []Widget{
			HSplitter{
				Children: []Widget{
					Label{Text: "用户名", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("Username"), AssignTo: &usr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					Label{Text: "密码", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("Password"), AssignTo: &pwd, MaxSize: Size{0, 20}, PasswordMode: true,
						OnMouseDown: func(x, y int, button walk.MouseButton) {
							pwd.SetPasswordMode(false)
						},
						OnMouseUp: func(x, y int, button walk.MouseButton) {
							pwd.SetPasswordMode(true)
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					Label{Text: "模式", MaxSize: Size{60, 20}},
					ComboBox{AssignTo: &mode,
						Editable: true, Value: Bind("RouterType"),
						Model:   []string{"local", "10.0.x.x(手动填写)", "openwrt", "hiwifi", "asus"},
						MaxSize: Size{0, 20},
						OnCurrentIndexChanged: func() {
							switch mode.CurrentIndex() {
							case 2: //"openwrt":
								laddr.SetVisible(true)
								lusr.SetVisible(true)
								lpwd.SetVisible(true)
							case 3: //"hiwifi":
								config.RouterAddr = "192.168.199.1"
								laddr.SetVisible(true)
								lusr.SetVisible(false)
								lpwd.SetVisible(true)
							case 4: //"asus":
								laddr.SetVisible(true)
								lusr.SetVisible(true)
								lpwd.SetVisible(true)
							default:
								laddr.SetVisible(false)
								lusr.SetVisible(false)
								lpwd.SetVisible(false)
							}
							mw.SetSize(walk.Size{0, 0})
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &laddr,
				Children: []Widget{
					Label{Text: "路由地址", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterAddr"), AssignTo: &raddr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &lusr,
				Children: []Widget{
					Label{Text: "路由用户名", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterUser"), AssignTo: &rusr, MaxSize: Size{0, 20}},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				AssignTo: &lpwd,
				Children: []Widget{
					Label{Text: "路由密码", MaxSize: Size{60, 20}},
					LineEdit{Text: Bind("RouterPwd"), AssignTo: &rpwd, MaxSize: Size{0, 20}, PasswordMode: true,
						OnMouseDown: func(x, y int, button walk.MouseButton) {
							rpwd.SetPasswordMode(false)
						},
						OnMouseUp: func(x, y int, button walk.MouseButton) {
							rpwd.SetPasswordMode(true)
						},
					},
				}, MaxSize: Size{0, 20},
			},
			HSplitter{
				Children: []Widget{
					PushButton{
						AssignTo: &lb,
						Text:     "开始连接",
						OnClicked: func() {
							if mode.Text() == "10.0.x.x(手动填写)" {
								walk.MsgBox(mw, "请填写IP", "手动填写需要自己获取IP 你可在路由器中自己查找 本地拨号请用local", walk.MsgBoxOK)
								return
							}
							lb.SetText("连接中...")
							lb.SetEnabled(false)
							rb.SetEnabled(false)
							go func() {
								d := netdialer.NewDialer(usr.Text(), pwd.Text())
								d.UserIP = selectMode(mode.Text())
								if d.UserIP == "" {
									walk.MsgBox(mw, "连接失败", "请检查设置", walk.MsgBoxOK)
									d = nil
									return
								}
								d.ConnectDirect()
								mw.SetTitle(TITLE + " [" + d.UserIP + "]")
								d = nil
								lb.SetEnabled(true)
								rb.SetEnabled(true)
								lb.SetText("开始连接")
								if err == nil {
									walk.MsgBox(mw, "连接成功", "感谢使用", walk.MsgBoxOK)
									util.INFO.Log("连接成功: 感谢使用")
								} else {
									walk.MsgBox(mw, "连接失败", err.Error(), walk.MsgBoxOK)
									util.INFO.Log("连接失败: ", err.Error())
								}
							}()
						},
					},
					PushButton{
						AssignTo: &rb,
						Text:     "断开连接",
						OnClicked: func() {
							if mode.Text() == "10.0.x.x(手动填写)" {
								walk.MsgBox(mw, "请填写IP", "手动填写需要自己获取IP 你可在路由器中自己查找", walk.MsgBoxOK)
								return
							}
							rb.SetText("断开中...")
							lb.SetEnabled(false)
							rb.SetEnabled(false)
							go func() {
								d := netdialer.NewDialer(usr.Text(), pwd.Text())
								d.UserIP = selectMode(mode.Text())
								if d.UserIP == "" {
									walk.MsgBox(mw, "连接失败", "请检查设置", walk.MsgBoxOK)
									util.INFO.Log("连接失败: 请检查设置")
									d = nil
									return
								}
								err := d.DisconnectDirect()
								mw.SetTitle(TITLE + " [" + d.UserIP + "]")
								d = nil
								lb.SetEnabled(true)
								rb.SetEnabled(true)
								rb.SetText("断开连接")
								if err == nil {
									walk.MsgBox(mw, "断开成功", "感谢使用", walk.MsgBoxOK)
									util.INFO.Log("断开成功: 感谢使用")
								} else {
									walk.MsgBox(mw, "断开失败", err.Error(), walk.MsgBoxOK)
									util.INFO.Log("断开失败: ", err.Error())
								}
							}()
						},
					},
				}, MaxSize: Size{0, 20},
			},
		},
	}.Run()
	saveConfig(config)
	bo.Flush()
}