Add: source code

This commit is contained in:
kota kanbe
2016-04-01 14:36:50 +09:00
parent 9ee9641a8a
commit f4fb0b5463
39 changed files with 7546 additions and 0 deletions

68
util/logutil.go Normal file
View File

@@ -0,0 +1,68 @@
/* Vuls - Vulnerability Scanner
Copyright (C) 2016 Future Architect, Inc. Japan.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package util
import (
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/rifflock/lfshook"
"github.com/future-architect/vuls/config"
formatter "github.com/kotakanbe/logrus-prefixed-formatter"
)
// NewCustomLogger creates logrus
func NewCustomLogger(c config.ServerInfo) *logrus.Entry {
log := logrus.New()
log.Formatter = &formatter.TextFormatter{MsgAnsiColor: c.LogMsgAnsiColor}
log.Out = os.Stderr
log.Level = logrus.InfoLevel
if config.Conf.Debug {
log.Level = logrus.DebugLevel
}
// File output
logDir := "/var/log/vuls"
if _, err := os.Stat(logDir); os.IsNotExist(err) {
if err := os.Mkdir(logDir, 0666); err != nil {
logrus.Errorf("Failed to create log directory: %s", err)
}
}
whereami := "localhost"
if 0 < len(c.ServerName) {
whereami = fmt.Sprintf("%s:%s", c.ServerName, c.Port)
}
if _, err := os.Stat(logDir); err == nil {
path := fmt.Sprintf("%s/%s.log", logDir, whereami)
log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
logrus.DebugLevel: path,
logrus.InfoLevel: path,
logrus.WarnLevel: path,
logrus.ErrorLevel: path,
logrus.FatalLevel: path,
logrus.PanicLevel: path,
}))
}
fields := logrus.Fields{"prefix": whereami}
return log.WithFields(fields)
}

120
util/util.go Normal file
View File

@@ -0,0 +1,120 @@
/* Vuls - Vulnerability Scanner
Copyright (C) 2016 Future Architect, Inc. Japan.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package util
import (
"fmt"
"net/url"
"strings"
"github.com/future-architect/vuls/config"
)
// GenWorkers generates goroutine
// http://qiita.com/na-o-ys/items/65373132b1c5bc973cca
func GenWorkers(num int) chan<- func() {
tasks := make(chan func())
for i := 0; i < num; i++ {
go func() {
for f := range tasks {
f()
}
}()
}
return tasks
}
// AppendIfMissing append to the slice if missing
func AppendIfMissing(slice []string, s string) []string {
for _, ele := range slice {
if ele == s {
return slice
}
}
return append(slice, s)
}
// URLPathJoin make URL
func URLPathJoin(baseURL string, paths ...string) (string, error) {
baseURL = strings.TrimSuffix(baseURL, "/")
trimedPaths := []string{}
for _, path := range paths {
trimed := strings.Trim(path, " /")
if len(trimed) != 0 {
trimedPaths = append(trimedPaths, trimed)
}
}
var url *url.URL
url, err := url.Parse(baseURL)
if err != nil {
return "", err
}
url.Path += strings.Join(trimedPaths, "/")
return url.String(), nil
}
// URLPathParamJoin make URL
func URLPathParamJoin(baseURL string, paths []string, params map[string]string) (string, error) {
urlPath, err := URLPathJoin(baseURL, paths...)
if err != nil {
return "", err
}
u, err := url.Parse(urlPath)
if err != nil {
return "", err
}
parameters := url.Values{}
for key := range params {
parameters.Add(key, params[key])
}
u.RawQuery = parameters.Encode()
return u.String(), nil
}
// ProxyEnv returns shell environment variables to set proxy
func ProxyEnv() string {
httpProxyEnv := "env"
keys := []string{
"http_proxy",
"https_proxy",
"HTTP_PROXY",
"HTTPS_PROXY",
}
for _, key := range keys {
httpProxyEnv += fmt.Sprintf(
` %s="%s"`, key, config.Conf.HTTPProxy)
}
return httpProxyEnv
}
// PrependProxyEnv prepends proxy enviroment variable
func PrependProxyEnv(cmd string) string {
if config.Conf.HTTPProxy == "" {
return cmd
}
return fmt.Sprintf("%s %s", ProxyEnv(), cmd)
}
// func unixtime(s string) (time.Time, error) {
// i, err := strconv.ParseInt(s, 10, 64)
// if err != nil {
// return time.Time{}, err
// }
// return time.Unix(i, 0), nil
// }

133
util/util_test.go Normal file
View File

@@ -0,0 +1,133 @@
/* Vuls - Vulnerability Scanner
Copyright (C) 2016 Future Architect, Inc. Japan.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package util
import (
"testing"
"github.com/future-architect/vuls/config"
)
func TestUrlJoin(t *testing.T) {
var tests = []struct {
in []string
out string
}{
{
[]string{
"http://hoge.com:8080",
"status",
},
"http://hoge.com:8080/status",
},
{
[]string{
"http://hoge.com:8080/",
"status",
},
"http://hoge.com:8080/status",
},
{
[]string{
"http://hoge.com:8080",
"/status",
},
"http://hoge.com:8080/status",
},
{
[]string{
"http://hoge.com:8080/",
"/status",
},
"http://hoge.com:8080/status",
},
{
[]string{
"http://hoge.com:8080/",
"/status",
},
"http://hoge.com:8080/status",
},
{
[]string{
"http://hoge.com:8080/",
"",
"hega",
"",
},
"http://hoge.com:8080/hega",
},
{
[]string{
"http://hoge.com:8080/",
"status",
"/fuga",
},
"http://hoge.com:8080/status/fuga",
},
}
for _, tt := range tests {
baseurl := tt.in[0]
paths := tt.in[1:]
actual, err := URLPathJoin(baseurl, paths...)
if err != nil {
t.Errorf("\nunexpected error occurred, err: %s,\ninput:%#v\nexpected: %s\n actual: %s", err, tt.in, tt.out, actual)
}
if actual != tt.out {
t.Errorf("\ninput:%#v\nexpected: %s\n actual: %s", tt.in, tt.out, actual)
}
}
}
func TestPrependHTTPProxyEnv(t *testing.T) {
var tests = []struct {
in []string
out string
}{
{
[]string{
"http://proxy.co.jp:8080",
"yum check-update",
},
`env http_proxy="http://proxy.co.jp:8080" https_proxy="http://proxy.co.jp:8080" HTTP_PROXY="http://proxy.co.jp:8080" HTTPS_PROXY="http://proxy.co.jp:8080" yum check-update`,
},
{
[]string{
"http://proxy.co.jp:8080",
"",
},
`env http_proxy="http://proxy.co.jp:8080" https_proxy="http://proxy.co.jp:8080" HTTP_PROXY="http://proxy.co.jp:8080" HTTPS_PROXY="http://proxy.co.jp:8080" `,
},
{
[]string{
"",
"yum check-update",
},
`yum check-update`,
},
}
for _, tt := range tests {
config.Conf.HTTPProxy = tt.in[0]
actual := PrependProxyEnv(tt.in[1])
if actual != tt.out {
t.Errorf("\nexpected: %s\n actual: %s", tt.out, actual)
}
}
}