Skip to content

Commit

Permalink
add accessTables support
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 21, 2016
1 parent 61758ec commit 2ab2c55
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: go
go:
- 1.5
- 1.6
- 1.7
env:
global:
- GO15VENDOREXPERIMENT=1
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ root -
Use config file. specfied with `--conf`, see [example config.yml](testdata/config.yml). Note that command line option can overwrite conf in `config.yml`
block visual some files, just add the following code to `.ghs.yml`
```yaml
accessTables:
- regex: block.file
allow: false
- regex: visual.file
allow: true
```

### ipa plist proxy
This is used for server which not https enabled. default use <https://plistproxy.herokuapp.com/plist>

Expand Down
38 changes: 35 additions & 3 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"strings"
"time"

"regexp"

"github.com/go-yaml/yaml"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -333,15 +335,42 @@ type HTTPFileInfo struct {
ModTime int64 `json:"mtime"`
}

type AccessTable struct {
Regex string `yaml:"regex"`
Allow bool `yaml:"allow"`
}

type AccessConf struct {
Upload bool `yaml:"upload" json:"upload"`
Delete bool `yaml:"delete" json:"delete"`
Upload bool `yaml:"upload" json:"upload"`
Delete bool `yaml:"delete" json:"delete"`
AccessTables []AccessTable `yaml:"accessTables"`
}

var reCache = make(map[string]*regexp.Regexp)

func (c *AccessConf) checkAccess(fileName string) bool {
for _, table := range c.AccessTables {
pattern, ok := reCache[table.Regex]
if !ok {
pattern, _ = regexp.Compile(table.Regex)
reCache[table.Regex] = pattern
}
// skip wrong format regex
if pattern == nil {
continue
}
if pattern.MatchString(fileName) {
return table.Allow
}
}
return true
}

func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
requestPath := mux.Vars(r)["path"]
localPath := filepath.Join(s.Root, requestPath)
search := r.FormValue("search")
auth := s.readAccessConf(requestPath)

// path string -> info os.FileInfo
fileInfoMap := make(map[string]os.FileInfo, 0)
Expand Down Expand Up @@ -370,6 +399,9 @@ func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
// turn file list -> json
lrs := make([]HTTPFileInfo, 0)
for path, info := range fileInfoMap {
if !auth.checkAccess(info.Name()) {
continue
}
lr := HTTPFileInfo{
Name: info.Name(),
Path: path,
Expand Down Expand Up @@ -397,7 +429,7 @@ func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {

data, _ := json.Marshal(map[string]interface{}{
"files": lrs,
"auth": s.readAccessConf(requestPath),
"auth": auth,
})
w.Header().Set("Content-Type", "application/json")
w.Write(data)
Expand Down
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Configure struct {
Conf *os.File `yaml:"-"`
Addr string `yaml:"addr"`
Root string `yaml:"root"`
HttpAuth string `yaml:"httpauth"`
HTTPAuth string `yaml:"httpauth"`
Cert string `yaml:"cert"`
Key string `yaml:"key"`
Cors bool `yaml:"cors"`
Expand All @@ -38,8 +38,7 @@ type Configure struct {
GoogleTrackerId string `yaml:"google-tracker-id"`
}

type logger struct {
}
type logger struct{}

func (l logger) Log(record accesslog.LogRecord) {
log.Printf("%s - %s %d %s", record.Ip, record.Method, record.Status, record.Uri)
Expand Down Expand Up @@ -92,7 +91,7 @@ func parseFlags() error {
kingpin.Flag("addr", "listen address, default :8000").Short('a').StringVar(&gcfg.Addr)
kingpin.Flag("cert", "tls cert.pem path").StringVar(&gcfg.Cert)
kingpin.Flag("key", "tls key.pem path").StringVar(&gcfg.Key)
kingpin.Flag("httpauth", "HTTP basic auth (ex: user:pass)").StringVar(&gcfg.HttpAuth)
kingpin.Flag("httpauth", "HTTP basic auth (ex: user:pass)").StringVar(&gcfg.HTTPAuth)
kingpin.Flag("theme", "web theme, one of <black|green>").StringVar(&gcfg.Theme)
kingpin.Flag("upload", "enable upload support").BoolVar(&gcfg.Upload)
kingpin.Flag("xheaders", "used when behide nginx").BoolVar(&gcfg.XHeaders)
Expand Down Expand Up @@ -146,7 +145,7 @@ func main() {
hdlr = accesslog.NewLoggingHandler(hdlr, l)

// HTTP Basic Authentication
userpass := strings.SplitN(gcfg.HttpAuth, ":", 2)
userpass := strings.SplitN(gcfg.HTTPAuth, ":", 2)
if len(userpass) == 2 {
user, pass := userpass[0], userpass[1]
hdlr = httpauth.SimpleBasicAuth(user, pass)(hdlr)
Expand Down
5 changes: 5 additions & 0 deletions testdata/deletable/.ghs.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
upload: true
delete: true
accessTables:
- regex: block.file
allow: false
- regex: visual.file
allow: true
Empty file added testdata/deletable/block.file
Empty file.
Empty file added testdata/deletable/other.file
Empty file.
Empty file added testdata/deletable/visual.file
Empty file.

0 comments on commit 2ab2c55

Please sign in to comment.