mirror of
				https://github.com/moul/sshportal.git
				synced 2025-11-01 00:46:12 +08:00 
			
		
		
		
	Session management
This commit is contained in:
		
							parent
							
								
									1fdf37dc07
								
							
						
					
					
						commit
						0fbcc0dd41
					
				
					 4 changed files with 87 additions and 5 deletions
				
			
		|  | @ -3,6 +3,7 @@ | |||
| ## master (unreleased) | ||||
| 
 | ||||
| * Create Session objects on each connections (history) | ||||
| * Session management | ||||
| 
 | ||||
| ## v1.4.0 (2017-11-24) | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										8
									
								
								db.go
									
										
									
									
									
								
							
							
						
						
									
										8
									
								
								db.go
									
										
									
									
									
								
							|  | @ -266,3 +266,11 @@ func UserRolesPreload(db *gorm.DB) *gorm.DB { | |||
| func UserRolesByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB { | ||||
| 	return db.Where("id IN (?)", identifiers).Or("name IN (?)", identifiers) | ||||
| } | ||||
| 
 | ||||
| // Session helpers | ||||
| func SessionsPreload(db *gorm.DB) *gorm.DB { | ||||
| 	return db.Preload("User").Preload("Host") | ||||
| } | ||||
| func SessionsByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB { | ||||
| 	return db.Where("id IN (?)", identifiers) | ||||
| } | ||||
|  |  | |||
							
								
								
									
										76
									
								
								shell.go
									
										
									
									
									
								
							
							
						
						
									
										76
									
								
								shell.go
									
										
									
									
									
								
							|  | @ -59,7 +59,7 @@ GLOBAL OPTIONS: | |||
| 	app.Commands = []cli.Command{ | ||||
| 		{ | ||||
| 			Name:  "acl", | ||||
| 			Usage: "Manages acls", | ||||
| 			Usage: "Manages ACLs", | ||||
| 			Subcommands: []cli.Command{ | ||||
| 				{ | ||||
| 					Name:        "create", | ||||
|  | @ -118,7 +118,7 @@ GLOBAL OPTIONS: | |||
| 					}, | ||||
| 				}, { | ||||
| 					Name:      "inspect", | ||||
| 					Usage:     "Shows detailed information on one or more acls", | ||||
| 					Usage:     "Shows detailed information on one or more ACLs", | ||||
| 					ArgsUsage: "ACL...", | ||||
| 					Action: func(c *cli.Context) error { | ||||
| 						if c.NArg() < 1 { | ||||
|  | @ -139,7 +139,7 @@ GLOBAL OPTIONS: | |||
| 					}, | ||||
| 				}, { | ||||
| 					Name:  "ls", | ||||
| 					Usage: "Lists acls", | ||||
| 					Usage: "Lists ACLs", | ||||
| 					Action: func(c *cli.Context) error { | ||||
| 						if err := UserCheckRoles(myself, []string{"admin"}); err != nil { | ||||
| 							return err | ||||
|  | @ -151,7 +151,7 @@ GLOBAL OPTIONS: | |||
| 						table := tablewriter.NewWriter(s) | ||||
| 						table.SetHeader([]string{"ID", "Weight", "User groups", "Host groups", "Host pattern", "Action", "Updated", "Created", "Comment"}) | ||||
| 						table.SetBorder(false) | ||||
| 						table.SetCaption(true, fmt.Sprintf("Total: %d acls.", len(acls))) | ||||
| 						table.SetCaption(true, fmt.Sprintf("Total: %d ACLs.", len(acls))) | ||||
| 						for _, acl := range acls { | ||||
| 							userGroups := []string{} | ||||
| 							hostGroups := []string{} | ||||
|  | @ -179,7 +179,7 @@ GLOBAL OPTIONS: | |||
| 					}, | ||||
| 				}, { | ||||
| 					Name:      "rm", | ||||
| 					Usage:     "Removes one or more acls", | ||||
| 					Usage:     "Removes one or more ACLs", | ||||
| 					ArgsUsage: "ACL...", | ||||
| 					Action: func(c *cli.Context) error { | ||||
| 						if c.NArg() < 1 { | ||||
|  | @ -1444,6 +1444,72 @@ GLOBAL OPTIONS: | |||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, { | ||||
| 			Name:  "session", | ||||
| 			Usage: "Manages sessions", | ||||
| 			Subcommands: []cli.Command{ | ||||
| 				{ | ||||
| 					Name:      "inspect", | ||||
| 					Usage:     "Shows detailed information on one or more sessions", | ||||
| 					ArgsUsage: "SESSION...", | ||||
| 					Action: func(c *cli.Context) error { | ||||
| 						if c.NArg() < 1 { | ||||
| 							return cli.ShowSubcommandHelp(c) | ||||
| 						} | ||||
| 
 | ||||
| 						if err := UserCheckRoles(myself, []string{"admin"}); err != nil { | ||||
| 							return err | ||||
| 						} | ||||
| 
 | ||||
| 						var sessions []Session | ||||
| 						if err := SessionsPreload(SessionsByIdentifiers(db, c.Args())).Find(&sessions).Error; err != nil { | ||||
| 							return err | ||||
| 						} | ||||
| 
 | ||||
| 						enc := json.NewEncoder(s) | ||||
| 						enc.SetIndent("", "  ") | ||||
| 						return enc.Encode(sessions) | ||||
| 					}, | ||||
| 				}, { | ||||
| 					Name:  "ls", | ||||
| 					Usage: "Lists sessions", | ||||
| 					Action: func(c *cli.Context) error { | ||||
| 						if err := UserCheckRoles(myself, []string{"admin"}); err != nil { | ||||
| 							return err | ||||
| 						} | ||||
| 
 | ||||
| 						var sessions []Session | ||||
| 						if err := db.Order("created_at desc").Preload("User").Preload("Host").Find(&sessions).Error; err != nil { | ||||
| 							return err | ||||
| 						} | ||||
| 						table := tablewriter.NewWriter(s) | ||||
| 						table.SetHeader([]string{"ID", "User", "Host", "Status", "Start", "Duration", "Error", "Comment"}) | ||||
| 						table.SetBorder(false) | ||||
| 						table.SetCaption(true, fmt.Sprintf("Total: %d sessions.", len(sessions))) | ||||
| 						for _, session := range sessions { | ||||
| 							var duration string | ||||
| 							if session.StoppedAt.IsZero() { | ||||
| 								duration = humanize.RelTime(session.CreatedAt, time.Now(), "", "") | ||||
| 							} else { | ||||
| 								duration = humanize.RelTime(session.CreatedAt, session.StoppedAt, "", "") | ||||
| 							} | ||||
| 							duration = strings.Replace(duration, "now", "1 second", 1) | ||||
| 							table.Append([]string{ | ||||
| 								fmt.Sprintf("%d", session.ID), | ||||
| 								session.User.Email, | ||||
| 								session.Host.Name, | ||||
| 								session.Status, | ||||
| 								humanize.Time(session.CreatedAt), | ||||
| 								duration, | ||||
| 								wrapText(session.ErrMsg, 30), | ||||
| 								session.Comment, | ||||
| 							}) | ||||
| 						} | ||||
| 						table.Render() | ||||
| 						return nil | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, { | ||||
| 			Name:  "version", | ||||
| 			Usage: "Shows the SSHPortal version information", | ||||
|  |  | |||
							
								
								
									
										7
									
								
								util.go
									
										
									
									
									
								
							
							
						
						
									
										7
									
								
								util.go
									
										
									
									
									
								
							|  | @ -11,3 +11,10 @@ func RandStringBytes(n int) string { | |||
| 	} | ||||
| 	return string(b) | ||||
| } | ||||
| 
 | ||||
| func wrapText(in string, length int) string { | ||||
| 	if len(in) <= length { | ||||
| 		return in | ||||
| 	} | ||||
| 	return in[0:length-3] + "..." | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue