Skip to content

Commit

Permalink
feat: wipログインが成功しない
Browse files Browse the repository at this point in the history
  • Loading branch information
kijimaD committed Oct 21, 2023
1 parent d298c9b commit f15af7c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/login.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package upl

import (
"errors"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os/exec"
"regexp"
"runtime"
Expand Down Expand Up @@ -54,3 +58,53 @@ func (t *Task) parseCookie(in string) (string, error) {
ans := re.FindAllStringSubmatch(in, -1)
return ans[0][1], nil
}

// ログインが成功しない
func (t *Task) login2() (string, error) {
form := url.Values{}
form.Add("fm_usr", "admin")
form.Add("fm_pwd", "admin@123")
body := strings.NewReader(form.Encode())
req, err := http.NewRequest("POST", t.baseurl, body)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "Go/1.20")
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{}

// クッキーの処理: クッキーを格納するためのジャーを作成
// jar, _ := cookiejar.New(nil)
// client.Jar = jar

resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

cookies := resp.Cookies()
var c string
for _, cookie := range cookies {
if cookie.Name == "filemanager" {
c = cookie.Value
}
}
// ログインに失敗するとトップページを返す
if len(c) == 0 {
return "", errors.New("クッキーを取得できなかった")
}
if resp.Status != "302 Found" {
return "", errors.New("ログインが成功しなかった")
}

dump, _ := httputil.DumpRequest(req, true)
fmt.Printf("%s", dump)
dump2, _ := httputil.DumpResponse(resp, true)
fmt.Printf("%s", dump2)

// ログインが成功してない
return c, nil
}
11 changes: 11 additions & 0 deletions pkg/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ func TestLogin(t *testing.T) {
}
assert.Equal(t, 26, len(cookie))
}

// できない
// func TestLogin2(t *testing.T) {
// buf := &bytes.Buffer{}
// task := NewTask(buf)
// cookie, err := task.login2()
// if err != nil {
// t.Error(err)
// }
// assert.Equal(t, 26, len(cookie))
// }
20 changes: 20 additions & 0 deletions pkg/upl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,23 @@ func TestUpload(t *testing.T) {

assert.Equal(t, `{"status":"success","info":"file upload successful"}`, resp)
}

// func TestUpload2(t *testing.T) {
// file, err := os.Create(UPLOAD_TARGET)
// if err != nil {
// t.Error(err)
// }
// defer os.Remove(file.Name())

// b := &bytes.Buffer{}
// task := NewTask(b)
// cookie, err := task.login2()
// if err != nil {
// t.Error(err)
// }
// resp, err := task.upload(cookie)
// if err != nil {
// t.Error(err)
// }
// assert.Equal(t, `{"status":"success","info":"file upload successful"}`, resp)
// }

0 comments on commit f15af7c

Please sign in to comment.