Skip to content

Commit 68769f1

Browse files
author
TP-O
committed
feat: validity check before registration
1 parent 7489ca9 commit 68769f1

File tree

5 files changed

+54
-92
lines changed

5 files changed

+54
-92
lines changed

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,16 @@ Usage of goer:
1616
-d Domain name (default "https://edusoftweb.hcmiu.edu.vn")
1717
-i List of course IDs
1818
-p Password
19-
-s Session ID
2019
-u Student ID
2120
```
2221

2322
## Example
2423
[How to get course ID](https://youtu.be/nPnCHI7AVZg)
2524

26-
### Use username and password
2725
```bash
2826
$ goer \
2927
-u ITITIU19180 \
3028
-p Mypassword \
3129
-i "IT092IU02 01|IT092IU|Principles of Programming Languages|02|4|0|01/01/0001|0|0|0| |0|ITIT19CS31" \
3230
-i "IT093IU02 01|IT093IU|Web Application Development|02|4|0|01/01/0001|0|0|0| |0|ITIT19CS31"
33-
```
34-
35-
### Use session ID
36-
```bash
37-
$ goer \
38-
-s SessionID \
39-
-i "IT092IU02 01|IT092IU|Principles of Programming Languages|02|4|0|01/01/0001|0|0|0| |0|ITIT19CS31" \
40-
-i "IT093IU02 01|IT093IU|Web Application Development|02|4|0|01/01/0001|0|0|0| |0|ITIT19CS31"
41-
```
31+
```

client.go

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,11 @@ import (
1212

1313
type Client struct {
1414
Host string
15-
Session string
1615
Http HttpInterface
1716
PayloadGenerator PayloadGeneratorInterface
1817
}
1918

20-
func (c *Client) CheckSession() bool {
21-
if url, err := url.Parse(c.Host); err == nil && c.Session != "" {
22-
c.Http.GetClient().Jar.SetCookies(url, []*http.Cookie{
23-
{
24-
Name: "ASP.NET_SessionId",
25-
Value: c.Session,
26-
HttpOnly: true,
27-
},
28-
})
29-
30-
return true
31-
}
32-
33-
return false
34-
}
35-
3619
func (c *Client) Login() (bool, string) {
37-
if c.CheckSession() {
38-
return true, "Session is used!!"
39-
}
40-
4120
path := "/default.aspx"
4221
payload := c.PayloadGenerator.LoginPayload()
4322

@@ -49,14 +28,26 @@ func (c *Client) Login() (bool, string) {
4928
} else if res.StatusCode != 302 || strings.Contains(res.Header.Values("Location")[0], "sessionreuse") {
5029
return false, "Login failed 😢 Trying to login again..."
5130
} else {
52-
if url, err := url.Parse(c.Host); err == nil {
53-
return true, "Login successfully!!! 😆 [" + c.Http.GetClient().Jar.Cookies(url)[0].Value + "]"
54-
}
55-
5631
return true, "Login successfully!!! 😆"
5732
}
5833
}
5934

35+
func (c *Client) Reset() (bool, string) {
36+
if url, err := url.Parse(c.Host); err == nil {
37+
c.Http.GetClient().Jar.SetCookies(url, []*http.Cookie{
38+
{
39+
Name: "ASP.NET_SessionId",
40+
Value: "",
41+
HttpOnly: true,
42+
},
43+
})
44+
45+
return true, "Logut successfully!!! 😆"
46+
}
47+
48+
return false, "Logout failed 😢"
49+
}
50+
6051
func (c *Client) SayHi() string {
6152
path := "/default.aspx"
6253

@@ -71,6 +62,24 @@ func (c *Client) SayHi() string {
7162
}
7263
}
7364

65+
func (c *Client) IsReady() (bool, string) {
66+
path := "/Default.aspx?page=dkmonhoc"
67+
68+
req, _ := http.NewRequest("GET", c.Host+path, nil)
69+
70+
if res, err := c.Http.Do(req); err != nil {
71+
return false, "Registration is not ready 😢"
72+
} else {
73+
document, _ := goquery.NewDocumentFromReader(res.Body)
74+
75+
if document.Find("#ctl00_ContentPlaceHolder1_ctl00_lblThongBaoNgoaiTGDK").Text() == "" {
76+
return true, "Registration is ready 😆"
77+
}
78+
79+
return false, "Registration is not ready 😢"
80+
}
81+
}
82+
7483
func (c *Client) Register(id string) (bool, string) {
7584
path := "/ajaxpro/EduSoft.Web.UC.DangKyMonHoc,EduSoft.Web.ashx"
7685
payload, course := c.PayloadGenerator.RegistrationPayload(id)
@@ -85,7 +94,7 @@ func (c *Client) Register(id string) (bool, string) {
8594
resBody, _ := io.ReadAll(res.Body)
8695

8796
if bytes.Contains(resBody, []byte(course)) {
88-
return true, "Registered"
97+
return true, "Registered 😆"
8998
}
9099

91100
return false, "Register failed 😢"

client_test.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,13 @@ var payloadGeneratorMock = PayloadGeneratorMock{}
5858
// Instance
5959
var client = Client{
6060
Host: "https://mock.com",
61-
Session: "session",
6261
Http: &httpMock,
6362
PayloadGenerator: &payloadGeneratorMock,
6463
}
6564

6665
// Cookie
6766
var jar, _ = cookiejar.New(nil)
6867

69-
func TestCheckSession(t *testing.T) {
70-
httpMock.On("GetClient").Return(&http.Client{Jar: jar})
71-
72-
assert.NotEmpty(t, client)
73-
74-
/* ============================= */
75-
ok := client.CheckSession()
76-
77-
assert.True(t, ok)
78-
79-
// Delete session for the next tests
80-
client.Session = ""
81-
82-
/* ============================= */
83-
ok = client.CheckSession()
84-
85-
assert.False(t, ok)
86-
}
87-
8868
func TestLogin(t *testing.T) {
8969
payload := Payload{
9070
Type: "xx",
@@ -95,19 +75,6 @@ func TestLogin(t *testing.T) {
9575

9676
assert.NotEmpty(t, client)
9777

98-
/* ============================= */
99-
client.Session = "session"
100-
101-
httpMock.On("GetClient").Return(&http.Client{Jar: jar})
102-
103-
ok, _ := client.Login()
104-
105-
assert.True(t, ok)
106-
httpMock.AssertNotCalled(t, "Do")
107-
108-
// Delete session for the next tests
109-
client.Session = ""
110-
11178
/* ============================= */
11279
httpMock.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{
11380
StatusCode: 302,
@@ -116,7 +83,7 @@ func TestLogin(t *testing.T) {
11683
},
11784
}, nil).Once()
11885

119-
ok, _ = client.Login()
86+
ok, _ := client.Login()
12087

12188
assert.True(t, ok)
12289

main.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bufio"
54
"os"
65
"runtime"
76
"strings"
@@ -12,17 +11,13 @@ import (
1211

1312
func main() {
1413
// Receive variables from CLI
15-
id, password, host, session, careful, courseId := RunCLI()
14+
id, password, host, careful, courseId := RunCLI()
1615
registeredId := []string{}
1716

1817
// Careful mode will ignore this saved condition
1918
// in the while loop below (always true).
2019
saved := careful
2120

22-
// Read input from console
23-
reader := bufio.NewReader(os.Stdin)
24-
input := ""
25-
2621
// Init logger
2722
logger := log.New(os.Stderr)
2823

@@ -33,9 +28,8 @@ func main() {
3328

3429
// Client
3530
client := Client{
36-
Host: host,
37-
Session: session,
38-
Http: NewHttp(),
31+
Host: host,
32+
Http: NewHttp(),
3933
PayloadGenerator: &PayloadGenerator{
4034
credentials: Credentials{
4135
ID: id,
@@ -49,20 +43,23 @@ func main() {
4943
if ok, message := client.Login(); ok {
5044
logger.Info(message)
5145

52-
break
46+
if isReady, isReadyMessage := client.IsReady(); isReady {
47+
logger.Info(isReadyMessage)
48+
49+
break
50+
} else {
51+
logger.Warn(isReadyMessage)
52+
logger.Warn("Logging in again...")
53+
54+
client.Reset()
55+
}
5356
} else {
5457
logger.Warn(message)
5558
}
5659
}
5760

58-
// Wait for user accepting
59-
for len(input) == 0 {
60-
logger.Info("Press enter to continue")
61-
input, _ = reader.ReadString('\n')
62-
}
63-
6461
// Get student ID
65-
// logger.Info(client.SayHi())
62+
logger.Info(client.SayHi())
6663

6764
// If careful mode is enabled, `!saved`` condition is always false.
6865
// Therefore, the condition to continue this while loop is

utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,21 @@ func CreateRegistrationBody(id string) (*bytes.Buffer, string) {
9595
return bytes.NewBuffer(byteBody), value[2]
9696
}
9797

98-
func RunCLI() (string, string, string, string, bool, []string) {
98+
func RunCLI() (string, string, string, bool, []string) {
9999
var courseId ArrayFlag
100100
id := flag.String("u", "", "Student ID")
101101
password := flag.String("p", "", "Password")
102102
host := flag.String("d", "https://edusoftweb.hcmiu.edu.vn", "Domain name")
103-
session := flag.String("s", "", "Session ID")
104103
careful := flag.Bool("c", false, "Save after each selection")
105104

106105
flag.Var(&courseId, "i", "List of course IDs")
107106
flag.Parse()
108107

109-
if (*id == "" || *password == "") && *session == "" {
108+
if *id == "" || *password == "" {
110109
fmt.Println("ID and Password are required!")
111110

112111
os.Exit(126)
113112
}
114113

115-
return *id, *password, *host, *session, *careful, courseId
114+
return *id, *password, *host, *careful, courseId
116115
}

0 commit comments

Comments
 (0)