proxyca.iced 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. {env} = require './env'
  2. fs = require 'fs'
  3. {make_esc} = require 'iced-error'
  4. {E} = require './err'
  5. log = require './log'
  6. #=========================================================================
  7. class ProxyCACert
  8. constructor : (@file) ->
  9. open : (cb) ->
  10. log.debug "| Load proxy CA: #{@file}"
  11. await fs.readFile @file, defer err, @raw
  12. cb err
  13. to_string : () -> @raw?.toString('utf8')
  14. #=========================================================================
  15. exports.ProxyCACerts = class ProxyCACerts
  16. constructor : () ->
  17. @_cas = []
  18. @_arr = []
  19. @_files = []
  20. #--------------------
  21. read_env : (cb) ->
  22. o = env().get_proxy_ca_certs()
  23. v = null
  24. err = null
  25. if not o? then # noop
  26. else if typeof(o) is 'string' then v = [ o ]
  27. else if typeof(o) is 'object' and Array.isArray(o) then v = o
  28. else
  29. err = new E.ArgsError "given CA list can't be parsed as list of files"
  30. if v?
  31. # Split each of the elements on ':' character
  32. # Then join all arrays into one array
  33. @_files = [].concat (e.split /:/ for e in v)...
  34. cb err
  35. #--------------------
  36. open_files : (cb) ->
  37. esc = make_esc cb, "CAs::open_files"
  38. @_cas = (new ProxyCACert(f) for f in @_files)
  39. for ca in @_cas
  40. await ca.open esc defer()
  41. cb null
  42. #--------------------
  43. load : (cb) ->
  44. log.debug "+ Load proxy CAs"
  45. esc = make_esc cb, "CAs::init"
  46. await @read_env esc defer()
  47. await @open_files esc defer()
  48. @_ca_arr = (c.to_string() for c in @_cas)
  49. log.debug "- Loaded proxy CAs"
  50. cb null, (@_cas.length > 0)
  51. #--------------------
  52. data : () -> @_ca_arr
  53. files : () -> @_files
  54. #=========================================================================
  55. _pcc = null
  56. exports.init = (cb) ->
  57. pcc = new ProxyCACerts()
  58. await pcc.load defer err, found
  59. if found and not err?
  60. _pcc = pcc
  61. cb err, _pcc
  62. #--------------
  63. exports.get = () -> _pcc
  64. #=========================================================================