Skip to content

Commit

Permalink
Merge pull request #2 from kijimaD/ci
Browse files Browse the repository at this point in the history
fix: crossplatform
  • Loading branch information
kijimaD committed Oct 21, 2023
2 parents dc957d0 + 53d845b commit 9fd0728
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 119 deletions.
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ services:
- .:/data
ports:
- 7777:80

win:
image: mcr.microsoft.com/powershell:latest
working_dir: /work
volumes:
- .:/work
extra_hosts:
- "host.docker.internal:host-gateway"
command: /bin/sh
124 changes: 84 additions & 40 deletions pkg/login.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,100 @@
package upl

import (
"errors"
"fmt"
"os/exec"
"regexp"
"runtime"
"io"
"net/http"
"net/url"
"strings"
)

const (
ADMIN_USER = "admin"
PWD = "admin@123"
)
// クッキーを生成する
func (t *Task) getCookie() (string, error) {
req, err := http.NewRequest("GET", t.baseurl, nil)
client := &http.Client{}

func (t *Task) buildLogin() string {
basecmds := []string{
"%s",
"%s",
"-s",
"-c -",
"-H 'Content-Type: application/x-www-form-urlencoded'",
"--data-raw 'fm_usr=%s&fm_pwd=%s'",
}
basecmd := strings.Join(basecmds, " ")
cmd := fmt.Sprintf(basecmd,
COMMAND,
t.baseurl,
ADMIN_USER,
PWD,
)
return cmd
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
}
}
return c, nil
}

// cookie jar textを返す
// 生成したクッキーでログインする
func (t *Task) login() (string, error) {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("powershell", "-Command", t.buildLogin())
default: // Linux & Mac
cmd = exec.Command("sh", "-c", t.buildLogin())
}
login, err := cmd.CombinedOutput()
values := url.Values{}
values.Add("fm_usr", t.adminuser)
values.Add("fm_pwd", t.pwd)

req, err := http.NewRequest("POST", t.baseurl, strings.NewReader(values.Encode()))
if err != nil {
fmt.Fprint(t.w, string(login), err)
return "", err
}
return string(login), nil
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

cookie, err := t.getCookie()
if err != nil {
return "", err
}
if len(cookie) == 0 {
return "", errors.New("クッキーを取得できなかった")
}
req.Header.Set("Cookie", fmt.Sprintf("filemanager=%s", cookie))

func (t *Task) parseCookie(in string) (string, error) {
re, _ := regexp.Compile("\tfilemanager\t([0-9a-zA-Z]+)")
ans := re.FindAllStringSubmatch(in, -1)
return ans[0][1], nil
client := &http.Client{}

resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.New("ログイン実行結果のステータスが200ではなかった")
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Sign Inが表示されているということはログイン画面にいる
if strings.Contains(string(b), "Sign in") {
return "", FailLoginError
}

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

return cookie, nil
}

// curl 'http://localhost:7777/index.php?p=' \
// -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
// -H 'Accept-Language: en-US,en;q=0.9,ja-JP;q=0.8,ja;q=0.7' \
// -H 'Cache-Control: max-age=0' \
// -H 'Connection: keep-alive' \
// -H 'Content-Type: application/x-www-form-urlencoded' \
// -H 'Cookie: filemanager=3ovivhi019m5313hdnmolqj7ku' \
// -H 'Origin: http://localhost:7777' \
// -H 'Referer: http://localhost:7777/index.php?p=' \
// -H 'Sec-Fetch-Dest: document' \
// -H 'Sec-Fetch-Mode: navigate' \
// -H 'Sec-Fetch-Site: same-origin' \
// -H 'Sec-Fetch-User: ?1' \
// -H 'Upgrade-Insecure-Requests: 1' \
// -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36' \
// -H 'sec-ch-ua: "Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"' \
// -H 'sec-ch-ua-mobile: ?0' \
// -H 'sec-ch-ua-platform: "Linux"' \
// --data-raw 'fm_usr=admin&fm_pwd=admin%40123' \
// --compressed
29 changes: 8 additions & 21 deletions pkg/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,22 @@ import (
"github.com/stretchr/testify/assert"
)

// Cookieを取得できる
func TestLogin(t *testing.T) {
buf := &bytes.Buffer{}
task := NewTask(buf)
str, err := task.login()
cookie, err := task.login()
if err != nil {
t.Error(err)
}

expect := `# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE / FALSE 0 filemanager `
assert.Contains(t, str, expect)
assert.Equal(t, 26, len(cookie))
}

func TestParseCookie(t *testing.T) {
// 認証情報が無効だとエラーを返す
func TestLoginFail(t *testing.T) {
buf := &bytes.Buffer{}
task := NewTask(buf)
input := `# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE / FALSE 0 filemanager dbf43855bb5bfc4a7fca5293ffa8da6b`
cookie, err := task.parseCookie(input)
if err != nil {
t.Error(err)
}
assert.Equal(t, 32, len(cookie))
task := NewTask(buf, TaskWithLoginUser("invalid user", "invalid pwd"))
cookie, err := task.login()

assert.Equal(t, FailLoginError, err)
assert.Equal(t, 0, len(cookie))
}
Loading

0 comments on commit 9fd0728

Please sign in to comment.