Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse f5vpn urls #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions cmd/gof5/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func main() {
fatal(err)
}

if flag.NArg() > 0 {
if err := client.UrlHandlerF5Vpn(&opts, flag.Arg(0)); err != nil {
fatal(err)
}
}

if err := client.Connect(&opts); err != nil {
fatal(err)
}
Expand Down
48 changes: 47 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,55 @@ type Options struct {
Sel bool
Version bool
ProfileIndex int
ProfileName string
Renegotiation tls.RenegotiationSupport
}

func UrlHandlerF5Vpn(opts *Options, s string) error {
u, err := url.Parse(s)
if err != nil {
return err
}

if u.Scheme != "f5-vpn" {
return fmt.Errorf("invalid scheme %v expected f5-vpn", u.Scheme)
}

m, err := url.ParseQuery(u.RawQuery)
if err != nil {
return err
}

resourceTypes := m["resourcetype"]
resourceNames := m["resourcename"]
if len(resourceTypes) == len(resourceNames) {
for i := range resourceTypes {
if resourceTypes[i] == "network_access" {
opts.ProfileName = resourceNames[i]
break
}
}
}

opts.Server = m["server"][0]
tokenUrl := fmt.Sprintf("%s://%s:%s/vdesk/get_sessid_for_token.php3", m["protocol"][0], opts.Server, m["port"][0])
request, err := http.NewRequest(http.MethodGet, tokenUrl, nil)
if err != nil {
return err
}
otc := m["otc"]
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("X-Access-Session-Token", otc[len(otc)-1])

response, err := http.DefaultClient.Do(request)
if err != nil {
return err
}

opts.SessionID = response.Header.Get("X-Access-Session-ID")
return nil
}

func Connect(opts *Options) error {
if opts.Server == "" {
fmt.Print("Enter server address: ")
Expand Down Expand Up @@ -152,7 +198,7 @@ func Connect(opts *Options) error {
return fmt.Errorf("wrong response code on profiles get: %d", resp.StatusCode)
}

profile, err := parseProfile(resp.Body, opts.ProfileIndex)
profile, err := parseProfile(resp.Body, opts.ProfileIndex, opts.ProfileName)
if err != nil {
return fmt.Errorf("failed to parse VPN profiles: %s", err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func login(c *http.Client, server string, username, password *string) error {
return nil
}

func parseProfile(reader io.ReadCloser, profileIndex int) (string, error) {
func parseProfile(reader io.ReadCloser, profileIndex int, profileName string) (string, error) {
var profiles config.Profiles
dec := xml.NewDecoder(reader)
err := dec.Decode(&profiles)
Expand All @@ -319,6 +319,9 @@ func parseProfile(reader io.ReadCloser, profileIndex int) (string, error) {
if profiles.Type == "VPN" {
prfls := make([]string, len(profiles.Favorites))
for i, p := range profiles.Favorites {
if profileName != "" && profileName == p.Name {
profileIndex = i
}
prfls[i] = fmt.Sprintf("%d:%s", i, p.Name)
}
log.Printf("Found F5 VPN profiles: %q", prfls)
Expand Down