Skip to content

Commit

Permalink
feat: add backend whitecats
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed May 17, 2021
1 parent edcfb95 commit b23f081
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
31 changes: 31 additions & 0 deletions apis/public/whitecats/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package whc

import (
"fmt"
"transfer/apis"
"transfer/utils"

"github.com/spf13/cobra"
)

var (
Backend = new(whiteCats)
)

type whiteCats struct {
apis.Backend
pwd string
del string
resp string
Commands [][]string
}

func (b *whiteCats) SetArgs(cmd *cobra.Command) {
cmd.Flags().StringVarP(&b.pwd, "password", "p", utils.GenRandString(4), "Set the download password")
cmd.Flags().StringVarP(&b.del, "delete", "d", utils.GenRandString(4), "Set the remove code")

cmd.Long = fmt.Sprintf("whiteCat Upload - http://whitecats.dip.jp/\n\n" +
utils.Spacer(" Size Limit: 100G\n") +
utils.Spacer(" Upload Service: NTT Communications\n") +
utils.Spacer(" Download Service: OCN/NTT Communications\n"))
}
8 changes: 8 additions & 0 deletions apis/public/whitecats/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package whc

// var matcher = regexp.MustCompile("whitecats.dip.jp/up/download/([0-9]{10})")

func (b whiteCats) LinkMatcher(v string) bool {
return false
// return matcher.MatchString(v)
}
17 changes: 17 additions & 0 deletions apis/public/whitecats/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package whc

import (
"io"
)

type uploadConfig struct {
debug bool
fileName string
fileReader io.Reader
fileSize int64

uploadName string
password string
delete string
fileid int64
}
151 changes: 151 additions & 0 deletions apis/public/whitecats/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package whc

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"mime/multipart"
"net/http"
"regexp"
"strconv"
"time"
"transfer/apis"
"transfer/utils"

"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)

const upload = "http://whitecats.dip.jp/up/upload/%d"
const download = "http://whitecats.dip.jp/up/download/%s"

var (
extact = regexp.MustCompile(`[0-9]{10}`)
errExtract = regexp.MustCompile(`error_message">(.*)</`)
)

func (b *whiteCats) DoUpload(name string, size int64, file io.Reader) error {

if b.pwd == "" {
b.pwd = utils.GenRandString(8)
}
if b.del == "" {
b.del = utils.GenRandString(8)
}

rand.Seed(time.Now().UnixNano())
body, err := b.newMultipartUpload(uploadConfig{
fileSize: size,
fileName: name,
fileReader: file,
debug: apis.DebugMode,
uploadName: utils.GenRandString(4) + ".bin",
password: b.pwd,
delete: b.del,
fileid: int64(rand.Int31()),
})

if err != nil {
return fmt.Errorf("upload returns error: %s", err)
}

b.resp = extact.FindString(string(body))
return nil
}

func (b *whiteCats) PostUpload(string, int64) (string, error) {
fmt.Printf("Download Link: %s\n", fmt.Sprintf(download, b.resp))
fmt.Printf("Download Password: %s\n", b.pwd)
fmt.Printf("Remove Code: %s\n", b.del)
return b.resp, nil
}

func (b whiteCats) newMultipartUpload(config uploadConfig) ([]byte, error) {
if config.debug {
log.Printf("start upload")
}
client := http.Client{}

byteBuf := &bytes.Buffer{}
writer := multipart.NewWriter(byteBuf)
_ = writer.WriteField("comment", config.fileName)
_ = writer.WriteField("download_pass", config.password)
_ = writer.WriteField("remove_pass", config.delete)
_ = writer.WriteField("code_pat", "京")
_ = writer.WriteField("submit", "ファイルを送信する")

_, err := writer.CreateFormFile("file", config.uploadName)
if err != nil {
return nil, err
}

writerLength := byteBuf.Len()
writerBody := make([]byte, writerLength)
_, _ = byteBuf.Read(writerBody)
_ = writer.Close()

lastBoundary := fmt.Sprintf("\r\n--%s--\r\n", writer.Boundary())
totalSize := int64(writerLength) + config.fileSize + int64(len(lastBoundary))
partR, partW := io.Pipe()

go func() {
_, _ = partW.Write(writerBody)
for {
buf := make([]byte, 256)
nr, err := io.ReadFull(config.fileReader, buf)
if nr <= 0 {
break
}
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
fmt.Println(err)
break
}
if nr > 0 {
_, _ = partW.Write(buf[:nr])
}
}
_, _ = fmt.Fprintf(partW, lastBoundary)
_ = partW.Close()
}()

req, err := http.NewRequest("POST", fmt.Sprintf(upload, config.fileid), partR)
if err != nil {
return nil, err
}
req.ContentLength = totalSize
req.Header.Set("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.9")
req.Header.Set("Referer", "http://whitecats.dip.jp/up/")

req.Header.Set("content-length", strconv.FormatInt(totalSize, 10))
req.Header.Set("content-type", fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary()))
if config.debug {
log.Printf("header: %v", req.Header)
}
resp, err := client.Do(req)
if err != nil {
if config.debug {
log.Printf("do requests returns error: %v", err)
}
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if config.debug {
log.Printf("read response returns: %v", body)
}
return nil, err
}
_ = resp.Body.Close()
str, _, _ := transform.String(japanese.EUCJP.NewDecoder(), string(body))
if config.debug {
log.Printf("returns: %v", str)
}
if p := errExtract.FindStringSubmatch(str); len(p) > 0 {
return nil, fmt.Errorf(p[1])
}

return body, nil
}
3 changes: 3 additions & 0 deletions cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
// "transfer/apis/public/wenshushu"
"transfer/apis/public/notion"
"transfer/apis/public/wetransfer"
whc "transfer/apis/public/whitecats"
)

var (
Expand All @@ -44,6 +45,7 @@ var (
{"0x0", "null"},
{"fio", "file.io"},
{"not", "notion", "notion.so"},
{"whc", "whitecat"},
}
baseBackend = []apis.BaseBackend{
cowtransfer.Backend,
Expand All @@ -62,6 +64,7 @@ var (
null.Backend,
fileio.Backend,
notion.Backend,
whc.Backend,
}
)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/spf13/cobra v1.1.3
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXR
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
Expand Down

0 comments on commit b23f081

Please sign in to comment.