doraemon 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d9c85bd3eb87662ed6053a4e0a1e649af527a6c5455b3593df390dcf9d76c3b
4
+ data.tar.gz: 42fb2a77a7327bb6944224f480c282f87787f7f16bb7b7dc416a887851b5bab1
5
+ SHA512:
6
+ metadata.gz: '0183784ade8020acca838281748b34f7edf262e2ad5d89961f378ababcea3f6d1e5bd0a96f1b39b60fdc05fb5328a6d501997dfa4227e58a8a326cc6fd2ee42a'
7
+ data.tar.gz: 57d5383930e326d66e0164ba6019dab24f41b204b0635fbd6273ab95c8c32c57f1fce3219023a867ca59b03ee3218315ad2ac359c3604316f1ab67e901514853
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Doraemon
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/doraemon`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'doraemon'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install doraemon
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/doraemon.
data/exe/doraemon ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ path = File.expand_path('../../lib/doraemon/utils', __FILE__)
4
+ $LOAD_PATH.unshift path
5
+
6
+ require 'doraemon'
7
+ require 'json'
8
+ require 'openssl'
9
+ require 'claide'
10
+
11
+ include Doraemon
12
+
13
+ argv = CLAide::ARGV.new(ARGV)
14
+ port = argv.option('port', 4000)
15
+ root = argv.option('root', Dir.pwd)
16
+
17
+ api_manager = Doraemon::APIManager.new(root)
18
+
19
+ proxy_server = Doraemon::ProxyServer.new(port.to_i, root)
20
+ proxy_server.start api_manager.load_api
21
+
22
+ http_server = Doraemon::HTTPServer.new(port.to_i+1, root)
23
+ http_server.start
@@ -0,0 +1,28 @@
1
+ module Doraemon
2
+
3
+ class APIManager
4
+
5
+ def initialize(root = Dir.pwd)
6
+ @root = root
7
+ end
8
+
9
+ def load_api
10
+
11
+ accept_exts = [".rb", ".json", ".api"]
12
+
13
+ api_list = {}
14
+
15
+ Dir.foreach(@root) do |filename|
16
+ next if !accept_exts.include? File.extname(filename).downcase
17
+ api_name = "/#{File.basename(filename, '.*')}".tr('_', '/')
18
+ api_list[api_name] = filename
19
+ puts "#{api_name} => #{filename}"
20
+ end
21
+
22
+ return api_list
23
+
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,45 @@
1
+ require 'webrick'
2
+ require 'cert'
3
+
4
+ module Doraemon
5
+
6
+ class HTTPServer
7
+
8
+ def initialize(port=8080, root)
9
+ @port = port
10
+ @root = root
11
+ end
12
+
13
+ def start
14
+
15
+ server = WEBrick::HTTPServer.new :Port => @port
16
+ server.mount_proc '/' do |req, resp|
17
+ if req.path == '/'
18
+ # 默认首页内容为下载证书页
19
+ resp.body << File.read(File.join(__dir__, "web/download_cert.html"))
20
+ resp.status = 200
21
+ else
22
+ path = File.join(@root, req.path)
23
+ if File.exist?(path)
24
+ resp.body << File.read(path)
25
+ resp.status = 200
26
+ else
27
+ resp.body << "#{req.path} Not Found"
28
+ resp.status = 404
29
+ end
30
+ end
31
+ end
32
+
33
+ server.mount_proc "/doraemon.crt" do |req, resp|
34
+ resp.body << File.read(Cert.cert_path)
35
+ resp.header["content-type"] = "application/x-x509-ca-cert"
36
+ resp.status = 200
37
+ end
38
+
39
+ trap 'INT' do server.shutdown end
40
+ server.start
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,74 @@
1
+ require 'ritm'
2
+ require 'json'
3
+ require 'cert'
4
+
5
+ module Doraemon
6
+
7
+ class ProxyServer
8
+
9
+ def initialize(port=8000, root)
10
+ @port = port
11
+ @root = root
12
+ end
13
+
14
+ def start(api_list)
15
+
16
+ Cert.generate_cert
17
+
18
+ session = Ritm::Session.new
19
+
20
+ port = @port
21
+
22
+ session.configure do
23
+ ssl_reverse_proxy.ca[:pem] = Cert.cert_path
24
+ ssl_reverse_proxy.ca[:key] = Cert.key_path
25
+ proxy[:bind_address] = '0.0.0.0'
26
+ proxy[:bind_port] = port
27
+ end
28
+
29
+ session.on_response do |_req, _resp|
30
+
31
+ if api_list.include?(_req.path)
32
+
33
+ _params = nil
34
+ begin
35
+ _params = JSON.parse(_req.body)
36
+ rescue
37
+ _params = _req.body
38
+ end
39
+
40
+ _result = JSON.parse(_resp.body)
41
+
42
+ begin
43
+ _result = eval(File.read(File.join(@root, api_list[_req.path])))
44
+ rescue
45
+ _result = {
46
+ "code": -1,
47
+ "msg": "#{_req.path} 处理错误"
48
+ }
49
+ end
50
+
51
+ _resp.status = 200
52
+ _resp.body = _result.to_json
53
+ _resp.header['content-length'] = _resp.body.bytesize
54
+ _resp.header['content-type'] = 'application/json;charset=UTF-8'
55
+
56
+ puts "- - - > #{_req.path}"
57
+ puts "#{JSON.pretty_generate(_result)}\n\n"
58
+ end
59
+
60
+ end
61
+
62
+ session.start
63
+
64
+ # trap 'INT' do
65
+ # session.shutdown
66
+ # return
67
+ # end
68
+ # loop { gets }
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 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dK.jpg",
74
+ "T3JV_TBCVv1RXrhCrK.jpg",
75
+ "T3Q_ATB7DT1RCvBVdK.jpg",
76
+ "T3X6DTBQbT1RXrhCrK.jpg",
77
+ "T3WvDTBT_T1RCvBVdK.jpg",
78
+ "T3ylZTB7AT1R4cSCrK.png",
79
+ "T3u9ZTB_DT1RXrhCrK.jpg",
80
+ "T3a9_TBgdT1RXrhCrK.jpg",
81
+ "T3xcATBKxT1RXrhCrK.jpg",
82
+ "T3JAATBgbT1RXrhCrK.jpg",
83
+ "T3ulDTB7KT1RXrhCrK.jpg",
84
+ "T3w_ZTB7hT1RXrhCrK.jpg",
85
+ "T34jZTB7LT1R4cSCrK.png",
86
+ "T3ZtLTBCKT1RXrhCrK.jpg",
87
+ "T3QD_TB4bT1RXrhCrK.jpg",
88
+ "T3Y7WTB4JT1RCvBVdK.jpg",
89
+ "T3LuJTBCYT1RXrhCrK.jpg",
90
+ "T3PrxTByCT1RXrhCrK.jpg",
91
+ "T32AVTBQxT1RCvBVdK.jpg",
92
+ "T3a1ZTB7AT1RXrhCrK.jpg",
93
+ "T309YTBsYT1RXrhCrK.jpg",
94
+ "T3pnETBXYT1R49Ip6K",
95
+ "T3B4xTBsCT1RCvBVdK.jpg",
96
+ "T3P1JTBvYT1RXrhCrK.jpg",
97
+ "T3y9bTBvDT1RXrhCrK.jpg",
98
+ "T3UPDTBXJT1RXrhCrK.jpg",
99
+ "T3ClDTBCAT1RXrhCrK.jpg",
100
+ "T3.wYTByWT1RXrhCrK.jpg",
101
+ "T3TJZTB4ET1RCvBVdK.jpg"
102
+ ]
@@ -0,0 +1,33 @@
1
+ require 'ritm/certs/ca'
2
+
3
+ module Doraemon
4
+
5
+ class Cert
6
+
7
+ def self.cert_root
8
+ File.join(Dir.home, ".doraemon")
9
+ end
10
+
11
+ def self.cert_path
12
+ File.join(Cert.cert_root, "doraemon.crt")
13
+ end
14
+
15
+ def self.key_path
16
+ File.join(Cert.cert_root, "doraemon.key")
17
+ end
18
+
19
+ def self.generate_cert
20
+ crt_root = Cert.cert_root
21
+ Dir.mkdir(crt_root) unless Dir.exist?(crt_root)
22
+
23
+ if !File.exist?(Cert.cert_path)
24
+ ca = Ritm::CA.create common_name: 'Doraemon'
25
+ File.write(Cert.cert_path, ca.pem)
26
+ File.write(Cert.key_path, ca.private_key.to_s)
27
+ puts 'doraemon.crt generate success.'
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,17 @@
1
+ require 'date'
2
+
3
+ class Date
4
+
5
+ def self.yesterday
6
+ Date.today.prev_day
7
+ end
8
+
9
+ def self.tomorrow
10
+ Date.today.next_day
11
+ end
12
+
13
+ def to_msec
14
+ self.to_time.to_i * 1000
15
+ end
16
+
17
+ end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+
3
+ module Doraemon
4
+
5
+ $image_list = JSON.parse(File.read(File.join(__dir__, '../res/image_source.json')))
6
+ $image_host = "https://file.kaipao.cc"
7
+
8
+ class DJImage
9
+ def self.random
10
+ path = $image_list.sample
11
+ "#{$image_host}/#{path}"
12
+ end
13
+
14
+ def self.at(index)
15
+ index = index % $image_list.length
16
+ path = $image_list[index]
17
+ "#{$image_host}/#{path}"
18
+ end
19
+
20
+ def self.local(path)
21
+
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module Doraemon
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,45 @@
1
+ <html>
2
+ <head>
3
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
4
+ <meta charset="UTF-8">
5
+ <link rel="icon" href="data:;base64,=">
6
+ <style type="text/css">
7
+ .main {
8
+ text-align: center;
9
+ background-color: white;
10
+ width: 200px;
11
+ height: 200px;
12
+ margin: auto;
13
+ position: absolute;
14
+ top: 0;
15
+ left: 0;
16
+ right: 0;
17
+ bottom: 0;
18
+ }
19
+ .download_btn {
20
+ background-color: #00CCFF;
21
+ border: none;
22
+ color: white;
23
+ padding: 15px 32px;
24
+ text-align: center;
25
+ text-decoration: none;
26
+ display: inline-block;
27
+ font-size: 20px;
28
+ border-radius: 12px;
29
+ width: 150px;
30
+ height: 75px;
31
+ box-shadow: 0 5px 10px rgba(0,0,0,0.2);
32
+ }
33
+ </style>
34
+ <script>
35
+ function download_cert() {
36
+ window.location.href = "doraemon.crt";
37
+ }
38
+ </script>
39
+ </head>
40
+ <body>
41
+ <div class="main">
42
+ <button class="download_btn" type="button" onClick="download_cert()">下载证书</button>
43
+ </div>
44
+ </body>
45
+ </html>
data/lib/doraemon.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'doraemon/version'
2
+
3
+ module Doraemon
4
+
5
+ require 'doraemon/utils/date'
6
+ require 'doraemon/api_manager'
7
+ require 'doraemon/http_server'
8
+ require 'doraemon/proxy_server'
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doraemon
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - zhuoyi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webrick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ritm
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: claide
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description: a simple API mock server.
84
+ email:
85
+ - jiangzhuoyi@gmail.com
86
+ executables:
87
+ - doraemon
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - README.md
92
+ - exe/doraemon
93
+ - lib/doraemon.rb
94
+ - lib/doraemon/api_manager.rb
95
+ - lib/doraemon/http_server.rb
96
+ - lib/doraemon/proxy_server.rb
97
+ - lib/doraemon/res/image_source.json
98
+ - lib/doraemon/utils/cert.rb
99
+ - lib/doraemon/utils/date.rb
100
+ - lib/doraemon/utils/djimage.rb
101
+ - lib/doraemon/version.rb
102
+ - lib/doraemon/web/download_cert.html
103
+ homepage: http://code.kaipao.cc/zhuoyi/doraemon
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.7.7
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: a simple API mock server
127
+ test_files: []