2023-11-07 00:21:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-11-08 10:20:26 -05:00
|
|
|
"html"
|
2023-11-07 00:21:48 -05:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-13 02:56:01 -05:00
|
|
|
func launch_aplus(course_code int) {
|
2023-11-14 04:23:18 -05:00
|
|
|
aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s",
|
|
|
|
base_link, course_code, external_tools_code, token)
|
2023-11-13 02:56:01 -05:00
|
|
|
aplus := get_aplus(token, aplus_link, client)
|
|
|
|
|
|
|
|
resp, _ := client.Get(aplus)
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
form_str := get_form_from_request_body(body)
|
|
|
|
form_values := parse_form(form_str)
|
|
|
|
|
|
|
|
resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values)
|
|
|
|
body, _ = io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
fmt.Println(string(body))
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:21:48 -05:00
|
|
|
func get_aplus(token string, link string, client http.Client) string {
|
2023-11-13 02:56:01 -05:00
|
|
|
resp, _ := client.Get(link)
|
2023-11-07 00:21:48 -05:00
|
|
|
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
var aplus Aplus
|
|
|
|
json.Unmarshal(body, &aplus)
|
|
|
|
|
|
|
|
return aplus.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func get_form_from_request_body(req_body []byte) string {
|
|
|
|
body_str := string(req_body)
|
|
|
|
form_start := strings.Index(body_str, "<form")
|
|
|
|
form_end := strings.Index(body_str, "</form>") + 7
|
|
|
|
form_html := req_body[form_start:form_end]
|
|
|
|
|
|
|
|
return string(form_html)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse_form extracts form fields and values from the given HTML form string.
|
|
|
|
func parse_form(form_html string) url.Values {
|
|
|
|
form_values := make(url.Values)
|
|
|
|
inputs := strings.Split(form_html, "<input")
|
|
|
|
|
|
|
|
for _, input := range inputs {
|
|
|
|
// Extract field name and value
|
|
|
|
name := extract_attribute(input, "name")
|
2023-11-08 10:20:26 -05:00
|
|
|
value := html.UnescapeString(extract_attribute(input, "value"))
|
2023-11-07 00:21:48 -05:00
|
|
|
|
|
|
|
if name != "" {
|
|
|
|
form_values.Add(name, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return form_values
|
|
|
|
}
|
|
|
|
|
|
|
|
func extract_attribute(input string, attribute string) string {
|
|
|
|
start := strings.Index(input, attribute+"=\"")
|
|
|
|
if start == -1 {
|
|
|
|
start = strings.Index(input, attribute+"='")
|
|
|
|
}
|
|
|
|
|
|
|
|
if start == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
start += len(attribute) + 2
|
|
|
|
|
|
|
|
end := strings.Index(input[start:], "\"")
|
|
|
|
if end == -1 {
|
|
|
|
end = strings.Index(input[start:], "'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if end == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return input[start : start+end]
|
|
|
|
}
|