Skip to content

Commit

Permalink
fix docker error
Browse files Browse the repository at this point in the history
  • Loading branch information
0c34 committed Sep 10, 2020
1 parent 97e6722 commit 0dee17e
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 124 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN go mod download
COPY . .

# Build the application
RUN go version
RUN go build -o main .

# Move to /dist directory as the place for resulting binary folder
Expand All @@ -33,7 +34,6 @@ COPY --from=builder /dist/main /
COPY ./config/config.json /config/config.json
COPY ./templates/* /templates/
COPY ./public/. /public/

EXPOSE 8888
# Command to run
ENTRYPOINT ["/main"]
CMD ["./main"]
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GoVWA is a vulnerable web application, **run the application on your local or te
#### Install golang
Install golang on you host

#### Install from source
#### Setup from source
```
git clone https://github.com/0c34/govwa.git
Expand Down Expand Up @@ -60,6 +60,18 @@ Open this url http://localhost:8888/ on your browser to access GoVWA
```
Open the url to access GoVWA and follow the setup instruction to create database and tables

#### Setup from docker
```
git clone https://github.com/0c34/govwa.git
inside govwa directory:
docker-compose up --build
stop running process using
docker-compose down --remove-orphans --volumes
```

GoVWA users:

|uname|password|
Expand Down
5 changes: 4 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func main() {
fmt.Printf("Server running at port %s\n", s.Addr)
fmt.Printf("Open this url %s on your browser to access GoVWA", config.Fullurl)
fmt.Println("")
s.ListenAndServe()
err := s.ListenAndServe()
if err != nil {
panic(err)
}

}
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ services:
build: .
ports:
- "8888:8888"
restart: on-failure
restart: always
volumes:
- .:/app
depends_on:
- db-mysql

db-mysql:
image: mysql:5.7
command: mysqld --sql_mode=""
container_name: db_mysql
ports:
- 3307:3306
Expand All @@ -21,6 +22,6 @@ services:
- MYSQL_USER=govwauser
- MYSQL_PASSWORD=govwaP@ss
- MYSQL_DATABASE=govwa
- MYSQL_ROOT_PASSWORD= govwaP@ss
- MYSQL_ROOT_PASSWORD=admin321


8 changes: 5 additions & 3 deletions setup/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package setup

import (
"database/sql"
"log"

"github.com/govwa/util/database"
)
Expand Down Expand Up @@ -36,14 +35,17 @@ const (
var DB *sql.DB
var err error

func init() {
/*func init() {
DB, err = database.Connect()
if err != nil {
log.Println(err.Error())
}
}
}*/

func createUsersTable() error {

DB, err = database.Connect()

_, err = DB.Exec(DropUsersTable)
if err != nil {
return err
Expand Down
70 changes: 36 additions & 34 deletions setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package setup

import(
import (
"fmt"
"net/http"

"github.com/govwa/util"
"github.com/govwa/user/session"
"github.com/govwa/util"
"github.com/govwa/util/config"
"github.com/govwa/util/database"
"github.com/govwa/util/middleware"
Expand All @@ -28,76 +28,78 @@ func (self Setup) SetRouter(r *httprouter.Router) {

}

type JsonResp struct{
Status string `json:"status"`
type JsonResp struct {
Status string `json:"status"`
Message string `json:"message"`
}

func setupViewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

func setupViewHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params){

/* clear login session when setup accessing setup page */
s := session.New()
s.DeleteSession(w, r)
cookies := []string{"Level", "Uid"}
util.DeleteCookie(w,cookies)
util.DeleteCookie(w, cookies)

var info string

data := make(map[string]interface{})
ok, err := database.CheckDatabase()
if !ok || err != nil{

if !ok || err != nil {
info = fmt.Sprintf(`<div id="info" class="alert alert-danger">%s
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`,err.Error())
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`, err.Error())
data["error"] = util.ToHTML(info)
}else{
} else {

info = fmt.Sprintf(`<div id="info" class="alert alert-success">Connection Success Click Reset to reset Database<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a></div>`)
data["error"] = util.ToHTML(info)
}

data["title"] = "Setup/Reset"
data["weburl"] = config.Fullurl
util.SafeRender(w,r,"template.setup", data)
util.SafeRender(w, r, "template.setup", data)
}

func setupActionHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

if r.Method == "POST" && r.FormValue("act") == "cr" {

func setupActionHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params){

if r.Method == "POST" && r.FormValue("act") == "cr"{

res := []JsonResp{}
loginUrl := util.ToHTML(fmt.Sprintf(`<a href=%slogin>Login</a>`,config.Fullurl))
loginUrl := util.ToHTML(fmt.Sprintf(`<a href=%slogin>Login</a>`, config.Fullurl))

err = createUsersTable() //create users table
if err != nil{
if err != nil {
res = append(res, JsonResp{
Status : "0",
Message : err.Error(),
Status: "0",
Message: err.Error(),
})
}else{
} else {
res = append(res, JsonResp{
Status : "1",
Message : "Create Users Table Success Please "+string(loginUrl),
Status: "1",
Message: "Create Users Table Success Please " + string(loginUrl),
})
}

err = createProfileTable() //create profilet table
if err != nil{

if err != nil {
res = append(res, JsonResp{
Status : "0",
Message : err.Error(),
Status: "0",
Message: err.Error(),
})
}else{
} else {
res = append(res, JsonResp{
Status : "1",
Message : "Create Profile Table Success Please "+string(loginUrl),
Status: "1",
Message: "Create Profile Table Success Please " + string(loginUrl),
})
}

res = append(res, JsonResp{
Status: "1",
Message: "sucess",
})
util.RenderAsJson(w, res)

}
}
}
55 changes: 28 additions & 27 deletions util/database/database.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
package database

import(
"fmt"
"log"
import (
"database/sql"
"github.com/govwa/util/config"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/govwa/util/config"
"log"
)

func Connect()(*sql.DB, error){
func Connect() (*sql.DB, error) {

config := config.LoadConfig()

var dsn string
var db *sql.DB

dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", config.User,config.Password,config.Sqlhost,config.Sqlport)
db, err := sql.Open("mysql",dsn)
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", config.User, config.Password, config.Sqlhost, config.Sqlport)
db, err := sql.Open("mysql", dsn)

if err != nil{
return nil,err
if err != nil {
return nil, err
}
_,err = db.Exec("CREATE DATABASE IF NOT EXISTS "+config.Dbname)
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS " + config.Dbname)

if err != nil{
return nil,err
}else{
if err != nil {
return nil, err
} else {

dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", config.User,config.Password,config.Sqlhost,config.Sqlport,config.Dbname)
db, err = sql.Open("mysql",dsn)
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", config.User, config.Password, config.Sqlhost, config.Sqlport, config.Dbname)
db, err = sql.Open("mysql", dsn)

if err != nil{
return nil,err
if err != nil {
return nil, err

}
}

return db,nil
return db, nil
}

var DB *sql.DB
func CheckDatabase()(bool, error){

func CheckDatabase() (bool, error) {

/* this function use to check if no database selected and will redirect to setup page */

DB, err := Connect()
if err != nil{
log.Printf("Connection Error %s ",err.Error())
if err != nil {
log.Printf("Connection Error %s ", err.Error())
}

const (
checksql = `SELECT 1 FROM Users limit 1` //this will check if Table dbname.Users exist otherways will redirect to setup page
checksql = `SELECT 1 FROM Users limit 1` //this will check if Table dbname.Users exist otherways will redirect to setup page
)
result, err := DB.Exec(checksql)

if err != nil{
if err != nil {
log.Println(err.Error())
return false,err
return false, err
}
if result == nil{
if result == nil {
return false, err
}
log.Println(result)
return true, nil
}
}
Loading

0 comments on commit 0dee17e

Please sign in to comment.