Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "log"
  5. "os/exec"
  6. "strings"
  7.  
  8. "github.com/abiosoft/ishell"
  9. "github.com/pkg/errors"
  10. "pkg.deepin.io/lib/dbus1"
  11. "pkg.deepin.io/lib/dbusutil"
  12. )
  13.  
  14. var globalAgents = make(map[string]*Agent)
  15.  
  16. func getAgent(id string) *Agent {
  17. return globalAgents[id]
  18. }
  19.  
  20. type Agent struct {
  21. name string
  22. shell *ishell.Shell
  23. methods *struct {
  24. RequestEchoOff func() `in:"msg" out:"result"`
  25. RequestEchoOn func() `in:"msg" out:"result"`
  26. DisplayErrorMsg func() `in:"msg"`
  27. DisplayTextInfo func() `in:"msg"`
  28. RespondResult func() `in:"cookie" out:"result"`
  29. }
  30. txObj dbus.BusObject
  31. }
  32.  
  33. func newAgent(id string, shell *ishell.Shell) *Agent {
  34. return &Agent{
  35. name: id,
  36. shell: shell,
  37. }
  38. }
  39.  
  40. func (ag *Agent) getPath() dbus.ObjectPath {
  41. return dbus.ObjectPath(dbusAgentPathPrefix + ag.name)
  42. }
  43.  
  44. const (
  45. authorityServiceName = "com.deepin.daemon.Authority"
  46. dbusAgentPathPrefix = "/user/swt/agent_"
  47. dbusAgentInterface = "com.deepin.daemon.Authority.Agent"
  48. )
  49.  
  50. func (*Agent) GetInterfaceName() string {
  51. return dbusAgentInterface
  52. }
  53.  
  54. func (ag *Agent) RequestEchoOff(msg string) (string, *dbus.Error) {
  55. ag.shell.Printf("RequestEchoOff: %q\n", msg)
  56. password, err := getSecretFromUI()
  57. if err != nil {
  58. ag.shell.Println("err:", err)
  59. return "", dbusutil.ToError(err)
  60. }
  61. ag.shell.Printf("password: %q\n", password)
  62. return password, nil
  63. }
  64.  
  65. func (ag *Agent) RequestEchoOn(msg string) (string, *dbus.Error) {
  66. ag.shell.Printf("RequestEchoOn: %q\n", msg)
  67. input, err := getStrFromUI(msg)
  68. if err != nil {
  69. ag.shell.Println("err:", err)
  70. return "", dbusutil.ToError(err)
  71. }
  72. ag.shell.Printf("input: %q\n", input)
  73. return input, nil
  74. }
  75.  
  76. func (ag *Agent) DisplayErrorMsg(msg string) *dbus.Error {
  77. ag.shell.Println("DisplayErrorMsg:", msg)
  78. return nil
  79. }
  80.  
  81. func (ag *Agent) DisplayTextInfo(msg string) *dbus.Error {
  82. ag.shell.Println("DisplayTextInfo:", msg)
  83. return nil
  84. }
  85.  
  86. func (ag *Agent) RespondResult(cookie string) *dbus.Error {
  87. ag.shell.Println("RespondResult:", cookie)
  88. if cookie == "" {
  89. ag.shell.Println("auth failed")
  90. }
  91. return nil
  92. }
  93.  
  94. var authorityObj dbus.BusObject
  95.  
  96. func authorityStart(authType, user string, dbusPath dbus.ObjectPath) (txObjPath dbus.ObjectPath, err error) {
  97. err = authorityObj.Call("com.deepin.daemon.Authority.Start", 0, authType, user, dbus.ObjectPath(dbusPath)).Store(&txObjPath)
  98. return
  99. }
  100.  
  101. func main() {
  102. service, err := dbusutil.NewSystemService()
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. authorityObj = service.Conn().Object(authorityServiceName, "/com/deepin/daemon/Authority")
  107.  
  108. shell := ishell.New()
  109.  
  110. shell.Println("i shell")
  111.  
  112. shell.AddCmd(&ishell.Cmd{
  113. Name: "start",
  114. Help: "start $authType $user $agentId",
  115. Func: func(c *ishell.Context) {
  116. if len(c.Args) != 3 {
  117. c.Err(errors.New("len args != 3"))
  118. return
  119. }
  120.  
  121. authType := c.Args[0]
  122. user := c.Args[1]
  123. agId := c.Args[2]
  124. c.Printf("authType: %q, user: %q, agentId: %q\n",
  125. authType, user, agId)
  126.  
  127. ag := getAgent(agId)
  128. if ag != nil {
  129. c.Err(errors.New("agent existed"))
  130. return
  131. }
  132.  
  133. ag = newAgent(agId, shell)
  134. agPath := ag.getPath()
  135. err := service.Export(agPath, ag)
  136. if err != nil {
  137. c.Err(err)
  138. return
  139. }
  140.  
  141. txObjPath, err := authorityStart(authType, user, agPath)
  142. if err != nil {
  143. c.Err(err)
  144.  
  145. // stop export ag
  146. err = service.StopExport(ag)
  147. if err != nil {
  148. c.Err(err)
  149. }
  150.  
  151. return
  152. }
  153. c.Println("tx obj:", txObjPath)
  154. ag.txObj = service.Conn().Object(authorityServiceName, txObjPath)
  155. globalAgents[agId] = ag
  156. },
  157. })
  158.  
  159. shell.AddCmd(&ishell.Cmd{
  160. Name: "auth",
  161. Help: "auth $agentId",
  162. Func: func(c *ishell.Context) {
  163. if len(c.Args) != 1 {
  164. c.Err(errors.New("len args != 1"))
  165. return
  166. }
  167. agId := c.Args[0]
  168.  
  169. ag := getAgent(agId)
  170. if ag == nil {
  171. c.Err(errors.New("agent not found"))
  172. return
  173. }
  174.  
  175. err = ag.txObj.Call(
  176. "com.deepin.daemon.Authority.Transaction.Authenticate", 0).Err
  177. if err != nil {
  178. c.Err(err)
  179. return
  180. }
  181. },
  182. })
  183.  
  184. shell.AddCmd(&ishell.Cmd{
  185. Name: "end",
  186. Help: "end $agentId",
  187. Func: func(c *ishell.Context) {
  188. if len(c.Args) != 1 {
  189. c.Err(errors.New("len args != 1"))
  190. return
  191. }
  192. agId := c.Args[0]
  193.  
  194. ag := getAgent(agId)
  195. if ag == nil {
  196. c.Err(errors.New("agent not found"))
  197. return
  198. }
  199.  
  200. err = ag.txObj.Call(
  201. "com.deepin.daemon.Authority.Transaction.End", 0).Err
  202. if err != nil {
  203. c.Err(err)
  204. return
  205. }
  206. },
  207. })
  208.  
  209. shell.AddCmd(&ishell.Cmd{
  210. Name: "set-user",
  211. Help: "set-user $agentId $user",
  212. Func: func(c *ishell.Context) {
  213. if len(c.Args) != 2 {
  214. c.Err(errors.New("len args != 2"))
  215. return
  216. }
  217. agId := c.Args[0]
  218.  
  219. ag := getAgent(agId)
  220. if ag == nil {
  221. c.Err(errors.New("agent not found"))
  222. return
  223. }
  224.  
  225. user := c.Args[1]
  226.  
  227. err = ag.txObj.Call(
  228. "com.deepin.daemon.Authority.Transaction.SetUser", 0, user).Err
  229. if err != nil {
  230. c.Err(err)
  231. return
  232. }
  233. },
  234. })
  235.  
  236. shell.AddCmd(&ishell.Cmd{
  237. Name: "ls",
  238. Help: "list agents",
  239. Func: func(c *ishell.Context) {
  240. for id, ag := range globalAgents {
  241. err = ag.txObj.Call("org.freedesktop.DBus.Properties.GetAll", 0,
  242. "com.deepin.daemon.Authority.Transaction").Err
  243. c.Printf("id: %q, path: %q, exist: %v\n",
  244. id, ag.txObj.Path(), err == nil)
  245. }
  246. },
  247. })
  248.  
  249. shell.Run()
  250. shell.Close()
  251. }
  252.  
  253. func getStrFromUI(msg string) (string, error) {
  254. out, err := exec.Command("zenity", "--entry", "--text="+msg).Output()
  255. if err != nil {
  256. return "", err
  257. }
  258. return strings.TrimSpace(string(out)), nil
  259. }
  260.  
  261. func getSecretFromUI() (string, error) {
  262. out, err := exec.Command("zenity", "--password").Output()
  263. if err != nil {
  264. return "", err
  265. }
  266. return strings.TrimSpace(string(out)), nil
  267. }
  268.  
  269. func getBoolFromUI(msg string) bool {
  270. err := exec.Command("zenity", "--question", "--text="+msg).Run()
  271. return err == nil
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement