This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,69 @@ | ||
package exp | ||
|
||
import ( | ||
"encoding/xml" | ||
"regexp" | ||
) | ||
import "fmt" | ||
|
||
type Result struct { | ||
type Context interface { | ||
GetHost() string | ||
GetUsername() string | ||
GetPassword() string | ||
Log(line string) | ||
} | ||
|
||
type ContextImpl struct { | ||
Host string | ||
Username string | ||
Password string | ||
Config []byte | ||
} | ||
|
||
type Exp struct { | ||
Name string | ||
Handle func(host string) (*Result, error) | ||
func NewContext(host string, username string, password string) Context { | ||
return &ContextImpl{ | ||
Host: host, | ||
Username: username, | ||
Password: password, | ||
} | ||
} | ||
|
||
func (_this *ContextImpl) GetHost() string { | ||
if _this.Host == "" { | ||
fmt.Printf(" - 请输入光猫地址 (默认: 192.168.1.1) : ") | ||
_, _ = fmt.Scanln(&_this.Host) | ||
if _this.Host == "" { | ||
_this.Host = "192.168.1.1" | ||
} | ||
} | ||
return _this.Host | ||
} | ||
|
||
func (_this *ContextImpl) GetUsername() string { | ||
if _this.Username == "" { | ||
fmt.Printf(" - 请输入普通管理员账号 (默认: useradmin) : ") | ||
_, _ = fmt.Scanln(&_this.Username) | ||
if _this.Username == "" { | ||
_this.Username = "useradmin" | ||
} | ||
} | ||
return _this.Username | ||
} | ||
|
||
type ModemConfig struct { | ||
LoginCfgAdminUserName string `xml:"InternetGatewayDevice>X_BROADCOM_COM_LoginCfg>AdminUserName"` | ||
TeleComAccountPassword string `xml:"InternetGatewayDevice>DeviceInfo>X_CT-COM_TeleComAccount>Password"` | ||
func (_this *ContextImpl) GetPassword() string { | ||
if _this.Password == "" { | ||
fmt.Printf(" - 请输入普通管理员密码 : ") | ||
_, _ = fmt.Scanln(&_this.Password) | ||
} | ||
return _this.Password | ||
} | ||
|
||
func parseModemConfig(data []byte) (*Result, error) { | ||
var modemConfig ModemConfig | ||
func (_this *ContextImpl) Log(line string) { | ||
fmt.Printf(" - %s\n", line) | ||
} | ||
|
||
// TRICKY: modem has invalid xml tag starts with number, replace it to valid one | ||
re := regexp.MustCompile("<(/?)([0-9]+)") | ||
s := re.ReplaceAllString(string(data), "<${1}fxxk${2}") | ||
type Result struct { | ||
Username string | ||
Password string | ||
Config []byte | ||
} | ||
|
||
err := xml.Unmarshal([]byte(s), &modemConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Result{ | ||
Username: modemConfig.LoginCfgAdminUserName, | ||
Password: modemConfig.TeleComAccountPassword, | ||
Config: data, | ||
}, nil | ||
type Exp struct { | ||
Name string | ||
Handle func(context Context) (*Result, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters