Skip to content
This repository has been archived by the owner on Aug 2, 2018. It is now read-only.

Commit

Permalink
use coreos/goproxy to fix ios websocket proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Jan 13, 2018
1 parent 9675863 commit 2befb88
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
- 运行方法二: 安装go(>=1.8)环境后, clone本repo源码到对应`$GOPATH/src/github.com/sundy-li/`下, 进入源码目录后,执行 `go run cmd/main.go`
- 手机wifi设置代理为pc的ip和端口,启动小程序王者头脑


## 问题

- ios端由于goproxy无法代理websocket问题,暂时无法使用,希望大家可以来完善这个问题,见[这个issue](https://github.com/sundy-li/wechat_brain/issues/18)
- 感谢@HsiangHo, @milkmeowo 的贡献,修复了ios代理问题,更新新版本后,最好重新安装证书,重启微信进程
~~ios端由于goproxy无法代理websocket问题,暂时无法使用,希望大家可以来完善这个问题,见[这个issue](https://github.com/sundy-li/wechat_brain/issues/18) ~~


## 合并题库
Expand Down
110 changes: 106 additions & 4 deletions cmd/question_utils.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,135 @@
package main

import (
"archive/zip"
"flag"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"

brain "github.com/sundy-li/wechat_brain"

"github.com/PuerkitoBio/goquery"
)

var (
action string
fs string
source string
fs string
issueUrl = "https://github.com/sundy-li/wechat_brain/issues/17"
tmpDir = "/data/tmp/"
)

func init() {
flag.StringVar(&action, "a", "show", "action value -> show | merge")
flag.StringVar(&source, "s", "show", "source value -> show | file | issue")
flag.StringVar(&fs, "fs", "", "merge data files")
flag.Parse()
}

func main() {
if action == "merge" {
if source == "file" {
files := strings.Split(fs, " ")
if len(files) < 1 {
log.Println("empty files")
return
}
brain.MergeQuestions(files...)
} else if source == "issue" {
doc, _ := goquery.NewDocument(issueUrl)
doc.Find("div.comment").Each(func(index int, comment *goquery.Selection) {
comment.Find("td.d-block p a").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), "questions.zip") {
href, _ := s.Attr("href")
if href != "" {
handleZipUrl(href)
}
}
})
})
}
total := brain.CountQuestions()
log.Println("total questions =>", total)
}

func handleZipUrl(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(tmpDir + "questions.zip")
if err != nil {
return err
}
defer out.Close()
io.Copy(out, resp.Body)
_, err = Unzip(tmpDir+"questions.zip", tmpDir)
if err != nil {
return err
}

//merge data
brain.MergeQuestions(tmpDir + "questions.data")
log.Println("merged", url)
return nil
}

// Unzip will un-compress a zip archive,
// moving all files and folders to an output directory
func Unzip(src, dest string) ([]string, error) {

var filenames []string

r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()

for _, f := range r.File {

rc, err := f.Open()
if err != nil {
return filenames, err
}
defer rc.Close()

// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
filenames = append(filenames, fpath)

if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)

} else {

// Make File
var fdir string
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}

err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
log.Fatal(err)
return filenames, err
}
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
defer f.Close()

_, err = io.Copy(f, rc)
if err != nil {
return filenames, err
}

}
}
return filenames, nil
}
Binary file modified questions.data
Binary file not shown.

0 comments on commit 2befb88

Please sign in to comment.