mirror of
https://github.com/kataras/iris.git
synced 2026-01-23 20:05:59 +00:00
Version 3.0.0-beta cleaned
This commit is contained in:
48
plugin/iriscontrol/README.md
Normal file
48
plugin/iriscontrol/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
## Iris Control
|
||||
|
||||
### THIS IS NOT READY YET
|
||||
|
||||
This plugin will give you remotely ( and local ) access to your iris server's information via a web interface
|
||||
|
||||
|
||||
### Assets
|
||||
No assets here because this is go -getable folder I don't want to messup with the folder size, in order to solve this
|
||||
I created a downloader manager inside this package which downloads the first time the assets and unzip them to the kataras/iris/plugin/iris-control/iris-control-assets/ .
|
||||
|
||||
|
||||
|
||||
The assets files are inside [this repository](https://github.com/iris-contrib/iris-control-assets)
|
||||
|
||||
|
||||
## How to use
|
||||
|
||||
```go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/plugin/iriscontrol"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
iris.Plugins().Add(iriscontrol.Web(9090, map[string]string{
|
||||
"irisusername1": "irispassword1",
|
||||
"irisusername2": "irispassowrd2",
|
||||
}))
|
||||
|
||||
iris.Get("/", func(ctx *iris.Context) {
|
||||
})
|
||||
|
||||
iris.Post("/something", func(ctx *iris.Context) {
|
||||
})
|
||||
|
||||
fmt.Printf("Iris is listening on :%d", 8080)
|
||||
iris.Listen(":8080")
|
||||
}
|
||||
|
||||
|
||||
|
||||
```
|
||||
105
plugin/iriscontrol/control_panel.go
Normal file
105
plugin/iriscontrol/control_panel.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package iriscontrol
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/plugin/routesinfo"
|
||||
)
|
||||
|
||||
var pathSeperator = string(os.PathSeparator)
|
||||
var pluginPath = os.Getenv("GOPATH") + pathSeperator + "src" + pathSeperator + "github.com" + pathSeperator + "kataras" + pathSeperator + "iris" + pathSeperator + "plugin" + pathSeperator + "iriscontrol" + pathSeperator
|
||||
var assetsURL = "https://github.com/iris-contrib/iris-control-assets/archive/master.zip"
|
||||
var assetsFolderName = "iris-control-assets-master"
|
||||
var installationPath = pluginPath + assetsFolderName + pathSeperator
|
||||
|
||||
// for the plugin server
|
||||
func (i *irisControlPlugin) startControlPanel() {
|
||||
|
||||
// install the assets first
|
||||
if err := i.installAssets(); err != nil {
|
||||
i.pluginContainer.Printf("[%s] %s Error %s: Couldn't install the assets from the internet,\n make sure you are connecting to the internet the first time running the iris-control plugin", time.Now().UTC().String(), Name, err.Error())
|
||||
i.Destroy()
|
||||
return
|
||||
}
|
||||
|
||||
i.server = iris.New()
|
||||
i.server.Config().Render.Template.Directory = installationPath + "templates"
|
||||
//i.server.SetRenderConfig(i.server.Config.Render)
|
||||
i.setPluginsInfo()
|
||||
i.setPanelRoutes()
|
||||
|
||||
go i.server.Listen(strconv.Itoa(i.options.Port))
|
||||
i.pluginContainer.Printf("[%s] %s is running at port %d with %d authenticated users", time.Now().UTC().String(), Name, i.options.Port, len(i.auth.authenticatedUsers))
|
||||
|
||||
}
|
||||
|
||||
// DashboardPage is the main data struct for the index
|
||||
// contains a boolean if server is running, the routes and the plugins
|
||||
type DashboardPage struct {
|
||||
ServerIsRunning bool
|
||||
Routes []routesinfo.RouteInfo
|
||||
Plugins []PluginInfo
|
||||
}
|
||||
|
||||
func (i *irisControlPlugin) setPluginsInfo() {
|
||||
plugins := i.pluginContainer.GetAll()
|
||||
i.plugins = make([]PluginInfo, 0, len(plugins))
|
||||
for _, plugin := range plugins {
|
||||
i.plugins = append(i.plugins, PluginInfo{Name: i.pluginContainer.GetName(plugin), Description: i.pluginContainer.GetDescription(plugin)})
|
||||
}
|
||||
}
|
||||
|
||||
// installAssets checks if must install ,if yes download the zip and unzip it, returns error.
|
||||
func (i *irisControlPlugin) installAssets() (err error) {
|
||||
//we know already what is the zip folder inside it, so we can check if it's exists, if yes then don't install it again.
|
||||
if i.pluginContainer.GetDownloader().DirectoryExists(installationPath) {
|
||||
return
|
||||
}
|
||||
//set the installationPath ,although we know it but do it here too
|
||||
installationPath, err = i.pluginContainer.GetDownloader().Install(assetsURL, pluginPath)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (i *irisControlPlugin) setPanelRoutes() {
|
||||
|
||||
i.server.Static("/public", installationPath+"static", 1)
|
||||
i.server.Get("/login", func(ctx *iris.Context) {
|
||||
ctx.Render("login", nil)
|
||||
})
|
||||
|
||||
i.server.Post("/login", func(ctx *iris.Context) {
|
||||
i.auth.login(ctx)
|
||||
})
|
||||
|
||||
i.server.Use(i.auth)
|
||||
i.server.Get("/", func(ctx *iris.Context) {
|
||||
ctx.Render("index", DashboardPage{ServerIsRunning: i.station.Server().IsListening(), Routes: i.routes.All(), Plugins: i.plugins})
|
||||
})
|
||||
|
||||
i.server.Post("/logout", func(ctx *iris.Context) {
|
||||
i.auth.logout(ctx)
|
||||
})
|
||||
|
||||
//the controls
|
||||
i.server.Post("/start_server", func(ctx *iris.Context) {
|
||||
//println("server start")
|
||||
old := i.stationServer
|
||||
if !old.IsSecure() {
|
||||
i.station.Listen(old.Config.ListeningAddr)
|
||||
//yes but here it does re- post listen to this plugin so ...
|
||||
} else {
|
||||
i.station.ListenTLS(old.Config.ListeningAddr, old.Config.CertFile, old.Config.KeyFile)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
i.server.Post("/stop_server", func(ctx *iris.Context) {
|
||||
//println("server stop")
|
||||
i.station.Close()
|
||||
})
|
||||
|
||||
}
|
||||
11
plugin/iriscontrol/index.go
Normal file
11
plugin/iriscontrol/index.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package iriscontrol
|
||||
|
||||
// NOT READY YET
|
||||
|
||||
// PluginInfo holds the Name and the description of the registed plugins
|
||||
type PluginInfo struct {
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
//func getPluginlist...
|
||||
112
plugin/iriscontrol/iriscontrol.go
Normal file
112
plugin/iriscontrol/iriscontrol.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package iriscontrol
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/config"
|
||||
"github.com/kataras/iris/plugin/routesinfo"
|
||||
"github.com/kataras/iris/server"
|
||||
)
|
||||
|
||||
// Name the name(string) of this plugin which is Iris Control
|
||||
const Name = "Iris Control"
|
||||
|
||||
type irisControlPlugin struct {
|
||||
options config.IrisControl
|
||||
// the pluginContainer is the container which keeps this plugin from the main user's iris instance
|
||||
pluginContainer iris.IPluginContainer
|
||||
// the station object of the main user's iris instance
|
||||
station *iris.Iris
|
||||
//a copy of the server which the main user's iris is listening for
|
||||
stationServer *server.Server
|
||||
|
||||
// the server is this plugin's server object, it is managed by this plugin only
|
||||
server *iris.Iris
|
||||
//
|
||||
//infos
|
||||
routes *routesinfo.Plugin
|
||||
plugins []PluginInfo
|
||||
//
|
||||
|
||||
auth *userAuth
|
||||
}
|
||||
|
||||
// New returns the plugin which is ready-to-use inside iris.Plugin method
|
||||
// receives config.IrisControl
|
||||
func New(cfg ...config.IrisControl) iris.IPlugin {
|
||||
c := config.DefaultIrisControl()
|
||||
if len(cfg) > 0 {
|
||||
c = cfg[0]
|
||||
}
|
||||
auth := newUserAuth(c.Users)
|
||||
if auth == nil {
|
||||
panic(Name + " Error: you should pass authenticated users map to the options, refer to the docs!")
|
||||
}
|
||||
|
||||
return &irisControlPlugin{options: c, auth: auth, routes: routesinfo.RoutesInfo()}
|
||||
}
|
||||
|
||||
// Web set the options for the plugin and return the plugin which is ready-to-use inside iris.Plugin method
|
||||
// first parameter is port
|
||||
// second parameter is map of users (username:password)
|
||||
func Web(port int, users map[string]string) iris.IPlugin {
|
||||
return New(config.IrisControl{port, users})
|
||||
}
|
||||
|
||||
// implement the base IPlugin
|
||||
|
||||
func (i *irisControlPlugin) Activate(container iris.IPluginContainer) error {
|
||||
i.pluginContainer = container
|
||||
container.Add(i.routes) // add the routesinfo plugin to the main server
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i irisControlPlugin) GetName() string {
|
||||
return Name
|
||||
}
|
||||
|
||||
func (i irisControlPlugin) GetDescription() string {
|
||||
return Name + " is just a web interface which gives you control of your Iris.\n"
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
// implement the rest of the plugin
|
||||
|
||||
// PostHandle
|
||||
func (i *irisControlPlugin) PostHandle(route iris.IRoute) {
|
||||
|
||||
}
|
||||
|
||||
// PostListen sets the station object after the main server starts
|
||||
// starts the actual work of the plugin
|
||||
func (i *irisControlPlugin) PostListen(s *iris.Iris) {
|
||||
//if the first time, because other times start/stop of the server so listen and no listen will be only from the control panel
|
||||
if i.station == nil {
|
||||
i.station = s
|
||||
i.stationServer = i.station.Server()
|
||||
i.startControlPanel()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (i *irisControlPlugin) PreClose(s *iris.Iris) {
|
||||
// Do nothing. This is a wrapper of the main server if we destroy when users stop the main server then we cannot continue the control panel i.Destroy()
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
// Destroy removes entirely the plugin, the options and all of these properties, you cannot re-use this plugin after this method.
|
||||
func (i *irisControlPlugin) Destroy() {
|
||||
i.pluginContainer.Remove(Name)
|
||||
|
||||
i.options = config.IrisControl{}
|
||||
i.routes = nil
|
||||
i.station = nil
|
||||
i.server.Close()
|
||||
i.pluginContainer = nil
|
||||
i.auth.Destroy()
|
||||
i.auth = nil
|
||||
i.pluginContainer.Printf("[%s] %s is turned off", time.Now().UTC().String(), Name)
|
||||
}
|
||||
20
plugin/iriscontrol/main_controls.go
Normal file
20
plugin/iriscontrol/main_controls.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package iriscontrol
|
||||
|
||||
// for the main server
|
||||
func (i *irisControlPlugin) StartServer() {
|
||||
if i.station.Server().IsListening() == false {
|
||||
if i.station.Server().IsSecure() {
|
||||
//listen with ListenTLS
|
||||
i.station.ListenTLS(i.station.Server().Config.ListeningAddr, i.station.Server().Config.CertFile, i.station.Server().Config.KeyFile)
|
||||
} else {
|
||||
//listen normal
|
||||
i.station.Listen(i.station.Server().Config.ListeningAddr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (i *irisControlPlugin) StopServer() {
|
||||
if i.station.Server().IsListening() {
|
||||
i.station.Close()
|
||||
}
|
||||
}
|
||||
97
plugin/iriscontrol/user_auth.go
Normal file
97
plugin/iriscontrol/user_auth.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package iriscontrol
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
// _ empty because it auto-registers
|
||||
_ "github.com/kataras/iris/sessions/providers/memory"
|
||||
)
|
||||
|
||||
var panelSessions *sessions.Manager
|
||||
|
||||
func init() {
|
||||
//using the default
|
||||
panelSessions = sessions.New()
|
||||
}
|
||||
|
||||
type user struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
type userAuth struct {
|
||||
authenticatedUsers []user
|
||||
}
|
||||
|
||||
// newUserAuth returns a new userAuth object, parameter is the authenticated users as map
|
||||
func newUserAuth(usersMap map[string]string) *userAuth {
|
||||
if usersMap != nil {
|
||||
obj := &userAuth{make([]user, 0)}
|
||||
for key, val := range usersMap {
|
||||
obj.authenticatedUsers = append(obj.authenticatedUsers, user{key, val})
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userAuth) login(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
username := ctx.PostFormValue("username")
|
||||
password := ctx.PostFormValue("password")
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
session.Set("username", username)
|
||||
session.Set("password", password)
|
||||
ctx.Write("success")
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Write("fail")
|
||||
|
||||
}
|
||||
|
||||
func (u *userAuth) logout(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
session.Set("user", nil)
|
||||
|
||||
ctx.Redirect("/login")
|
||||
}
|
||||
|
||||
// check if session stored, then check if this user is the correct, each time, then continue, else not
|
||||
func (u *userAuth) Serve(ctx *iris.Context) {
|
||||
if ctx.PathString() == "/login" || strings.HasPrefix(ctx.PathString(), "/public") {
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
if sessionVal := session.Get("username"); sessionVal != nil {
|
||||
username := sessionVal.(string)
|
||||
password := session.GetString("password")
|
||||
if username != "" && password != "" {
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
ctx.Next()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//if not logged in the redirect to the /login
|
||||
ctx.Redirect("/login")
|
||||
|
||||
}
|
||||
|
||||
// Destroy this is called on PreClose by the iriscontrol.go
|
||||
func (u *userAuth) Destroy() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user